user3732216
user3732216

Reputation: 1589

Select Option Groups

I currently have the following query where I grab all users that have a role of 1,2,3 in the database. I grab their first name and last name and id for the sole purpose of a select box. What I'm wanting to do is with the ids that I pass in the whereIn to also grab the role name so I can use it for creating option groups.

$options = \App\User::whereHas('roles', function ($query) {
            $query->whereIn('id', [1, 2, 3]);
        })->get()->map(function ($user) {
            $user->name = $user->first_name . ' ' . $user->last_name;
            return $user;
        })->pluck('name', 'id');

UPDATE:

Keep in mind that this my table structure.

Users Table- id, first_name, last_name 
Role_User Table = role_id, user_id 
Roles Table - id, name

I'm trying to do the following as an example.

<select name="assign_id">
    <optgroup label="Admins">
            <option value="1">John Smith</option>
            <option value="2">Jane Smith</option>
    </optgroup>
    <optgroup label="Editors">
            <option value="3">Brian Smith</option>
            <option value="4">Scott Smith</option>
    </optgroup>
    <optgroup label="Basic Users">
            <option value="3">Kevin Smith</option>
            <option value="4">Tanya Smith</option>
    </optgroup>
</select>

Upvotes: 0

Views: 962

Answers (3)

Saumya Rastogi
Saumya Rastogi

Reputation: 13703

You can do it by making the use of with method like this.

$options = \App\User::with('roles')
        ->whereHas('roles', function ($query) {
            $query->whereIn('id', [1, 2, 3]);
        })->get()->map(function ($user) {
            $user->name = $user->first_name . ' ' . $user->last_name;
            $user->role_name = $user->roles->first()->name;
            return $user;
        }) // Or to make it more usable you can use 'groupBy' on Collections
        ->groupBy('role_name')
        ->toArray();

/* The result will be like...
    [
        'role_1' => [
            User Array 1,
            User Array 2,
        ],
        'role_2' => [
            User Array 3,
        ],
        ...
    ]
*/

This will give you user name, id and the role name of a particular user

Upvotes: 1

Omid
Omid

Reputation: 93

If you want to get a list of roles for each person, it's better to use this: In your UserController.php in index method add this

$userRole = User::with('roles')->get();

return view('users.index')->withUserRole($userRole);

and in your user/index.blade.php add this to your content

@foreach($userRole as $user)
    <li>
        {!! $user->first_name !!}
        @foreach($user->roles as $role )
                <ol>
                    {!! link_to_route('users.show', $role->role_name,[$role->id]) !!}
                </ol>
        @endforeach
    </li>
@endforeach

Upvotes: 0

Omid
Omid

Reputation: 93

you can actually use

$options = \App\User::with('roles')->whereId(array('1', '2', '3'))->pluck('name', 'id');

with is when you have a relationship and want to join with that and after with you can put your conditions ( where conditions) that are related to the joined table

Upvotes: 0

Related Questions