In the controller, you also need to load all the roles:
$users = User::with('roles')->get();
$roles = Role::get();
return view('admin', compact('users', 'roles'));
And in the view, do something like this:
@foreach($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
@foreach($roles as $role)
<td><input type="checkbox" name="role[]" {{ $user->roles->contains($role) ? 'checked' : '' }}></td>
@endforeach
<td></td>
</tr>
@endforeach
So, you need to iterate over all roles, not just the roles that the user has.
source
share