I have this basic database model I would like to group all users by role name i.e. I want to list the users who are admins and the others in two collections.
Database model
I tried to do this but it only works for one to many relationships
User::with('roles’)->get()->groupBy(‘roles.name’);
CodePudding user response:
Use wildcard *
to skip an array:
User::with('roles')->get()->groupBy('roles.*.name');