Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Bromira's avatar

Laratrust - List Members of Team

Hi,

My question is specific to Laratrust in this instance but would be handy to know generally.

In Laratrust we have the following tables:

Permissions

  • id
  • name
  • display_name
  • description
  • created at
  • updated at

Roles

  • id
  • name
  • display_name
  • description
  • created at
  • updated at

role_user

  • role_id
  • user_id
  • user_type (Model - App\User in my case)
  • team_id

Permission_role

  • permission_id
  • role_id

Teams

  • id
  • name
  • display_name
  • description
  • created at
  • updated at

Users

  • id
  • username
  • email
  • password
  • created at
  • updated at

Users can be assigned to multiple teams at different levels (ie. User 1 could be in Team 1 as an Admin and Team 2 as a user and User 2, 3 & 4 could be a mixture of User/Admin/Editor of a mixture of different roles within the teams) admins already have the right to view and edit "users" within teams that they're admin of.

I was wondering if there is an efficient way to list the Users that are in each team the current Auth user is assigned to as Admin (including duplicate users across teams - ie. User 1 listed in Team 1 & Team 2 if they have been assigned to both)?

In straight PHP/MYSQL I would probably select all the records from role_user where the user_id = current user & role_id matched the role id for Admin then join the table on itself with Team_id and additionally join the Users table on user_id.

Is there a better way in Eloquent or an efficient way to write the same query in Laravel?

Thank you!

0 likes
2 replies
cotton09's avatar
//This will get all members of a team that belongs to any role
$team  = Team::where('name', $id)->first();
$roles = Role::get();
$users = User::whereRoleIs($roles->pluck('name')->toArray(), $team)->get();

//This will get all members of a team with a specific role
$team  = Team::where('name', $id)->first();
$users = User::whereRoleIs(['Admin'], $team)->get();
priyalaks's avatar

@bromira - Did you figure out on how to list all the users who belong to a specific team. ?

Please or to participate in this conversation.