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>