JosephSilber / bouncer

Laravel Eloquent roles and abilities.
MIT License
3.43k stars 330 forks source link

Get all users with all their roles #642

Closed FunctionDJ closed 9 months ago

FunctionDJ commented 9 months ago

Hi, i'd like to run a query using a JOIN from the users table over the assigned_roles and roles tables so that i can get all this information with a single query. I feel like the docs of Bouncer only cover either getting all users of a certain role or the roles of individual users, which would be an N+1 issue. Does Bouncer have a shortcut for this or should i just write a manual JOIN / with?

JosephSilber commented 9 months ago

You can do it with two queries, using the standard Eloquent relationship:

User::with('roles')->get();
FunctionDJ commented 9 months ago

@JosephSilber Works nicely, thank you very much!