Level 5
Does a user have multiple roles? Otherwise you could set up a relationship and acces it like $user->role in your loop.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have problem with user roles, which i'd like to send to the view and display it.
Controller
public function manage(Request $request)
{
//Check role
$request->user()->authorizeRoles('admin');
$users = User::all();
$roles = Role::all();
return view('users.manage')
->with('users', $users)
->with('roles', $roles);
}
View
@foreach($users as $index => $user)
<tr>
<td>{{ $index +1}}</td>
<td>{{$user->name}}</td>
<td>{{$user->surname}}</td>
<td>ROLE HERE</td>
<td>{{$user->department}}</td>
</tr>
</tbody>
@endforeach
Please or to participate in this conversation.