Skip to content

How to get list of all user with each roles #1678

Answered by drbyte
ridlafalah asked this question in Q&A
Discussion options

You must be logged in to vote

Unlike a username, users can have more than one role, so you'll need to deal with the collection.

$users = User::with(['roles'])->get();

foreach($users as $user) {
  // $user->name;
  // $user->username;
  // $user->getRoleNames(); // collection of names of roles assigned to the user
     // $roles = implode(', ', $user->getRoleNames()); // comma-separated list of role names
}

These are all Eloquent features, so normal Laravel Eloquent capabilities exist.
See https://spatie.be/docs/laravel-permission/v4/basic-usage/basic-usage#eloquent

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by drbyte
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants