-
-
Notifications
You must be signed in to change notification settings - Fork 377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to get a collection of users in this role? #44
Comments
As far as I know this has not been explained in the documentation, but it's fairly simple. The public function posts()
{
return $this->belongsToMany(Post::class);
} Our case is exactly the same, meaning: $user = User::first();
$roles = $user->roles; // Get all user roles.
// or
$roles = $user->roles()->get(); and $role = Role::first();
$users = $role->users; // Get all users with this role assigned to them
// or
$users = $role->users()->get(); I hope this explains it for you. By the way, this is an open source project, so you are more than engouraged to take a look at the package files. public function users()
{
return $this->belongsToMany(
App\User::class,
'role_user',
'role_id',
'user_id'
);
} which is just an explicit way of saying public function users()
{
return $this->belongsToMany(App\User::class);
} As you can see there's not much magic going on. |
Thank you! Working! $role = Role::where('name','=','administrator')->first(); |
Hello!
How to get a collection of users in this role?
Thank you
The text was updated successfully, but these errors were encountered: