Reputation: 2406
I have the index function on my app, that shows a table with all users:
public function index(Request $request)
{
$this->authorize('inet.user.view');
$users = User::with(['roles', 'groups'])->paginate(10);
return view('security.users.index', compact('users'));
}
But when I try do make the list of groups and roles, i have many errors. This is table:
<table class="table table-responsive" id="users-table">
<thead>
<th>
@lang('table.generic.name')
</th>
<th>
@lang('table.users.role') & @lang('table.users.group')
</th>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>
{!! $user->fullname !!}
</td>
<td>
{!! ---HERE NEED TO SHOW LIST OF ROLES--- !!}
</td>
</tr>
@endforeach
</tbody>
</table>
Im trying to join with implode(', ', theMethod)
and get the names with array_pluck($user, 'roles.name')
but doesn't work and $user->pluck('roles.name')
neither.
How can i get the list of roles and groups without doing a for
in the view?
Upvotes: 2
Views: 3026
Reputation: 163748
Just iterate over nested collections loaded with the with()
method:
@foreach($user->roles as $role)
{{ $role->name }}
@endforeach
@foreach($user->groups as $group)
{{ $group->name }}
@endforeach
If you want to use implode()
, use pluck()
to get names:
implode(', ', $user->roles->pluck('name')->toArray())
Upvotes: 5