0

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>
user3732216
  • 1,579
  • 8
  • 29
  • 54

3 Answers3

1

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

Saumya Rastogi
  • 13,159
  • 5
  • 42
  • 45
0

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

Omid
  • 93
  • 3
  • 12
0

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
Omid
  • 93
  • 3
  • 12
  • This would be a great answer if it was something that achieved my desired results. – user3732216 Nov 04 '16 at 00:55
  • do you have a table called "roles" with pivot table called "user_role"?! – Omid Nov 04 '16 at 00:58
  • I do. However remember this is for a select drop down. So for an option group I need it to show the role name and then for each user with that role it will display the name and if as the value of the option. Then when all of the users are looped through for that role it makes a new option group with the next role name and then loops through again. – user3732216 Nov 04 '16 at 01:01
  • what i said was for a none-pivot table but if you have a pivot table use sync() http://stackoverflow.com/questions/27230672/laravel-sync-how-to-sync-an-array-and-also-pass-additional-pivot-fields – Omid Nov 04 '16 at 01:07
  • You aren't understanding. This is for a select drop down. I'm. It saving anything to the database. – user3732216 Nov 04 '16 at 01:27
  • I have updated my original post with my explanation. – user3732216 Nov 04 '16 at 13:58