Level 47
Because it's a many to many.
User::with('roles')->get();
Then
$user->roles as $role loop
Or $user->roles->first(); if you plan on a user having one role
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
I made a list of all my users within the database. Now I want to show their role too. How would I do that? I can only get the role of the current authenticated user.
My code to output all the users (currently 2):
@foreach($users->all() as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->first_name }}</td>
<td>{{ $user->last_name }}</td>
<td><a href="{{ route('profile', [$user->username]) }}">{{ $user->username }}</a></td>
<td><a href="mailto:{{ $user->getNameOrUsername() }}<{{ $user->email }}>">{{ $user->email }}</a></td>
<td></td>
<td>{{ $user->created_at }}</td>
<td>{{ $user->updated_at }}</td>
<td><a href="{{ route('admin.manage_user', $user->username) }}" class="btn btn-info btn-xs"><i class="fa fa-pencil"></i></a></td>
</tr>
@endforeach
Because it's a many to many.
User::with('roles')->get();
Then
$user->roles as $role loop
Or $user->roles->first(); if you plan on a user having one role
Please or to participate in this conversation.