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

abirone's avatar

How do I get all users on a team with a specific role?

I have two Roles in my SparkServiceProvider;

Spark::useRoles([  
  'owner' => 'Owner',  
  'professional' => 'Professional',
]);

I need the ability to fetch all members of the current team with the "professional" role. I can't find any example of this in the Spark documentation.

Thanks in advance!

0 likes
2 replies
Braunson's avatar

Something like this.. at least for Spark.. we query the pivot table team_users

$users = User::whereHas('teams', function($query) {
    $query->where('team_users.role', '=', 'professional');
})->distinct()->get();
1 like
abirone's avatar
abirone
OP
Best Answer
Level 2

Thanks for the reply! I ended up finding this is Spark\Team;

/**  
 * Get all of the users that belong to the team. */public function users()  
{  
  return $this->belongsToMany(  
        Spark::userModel(), 'team_users', 'team_id', 'user_id'  
  )->withPivot('role');  
}

So, I customized it and brought it in to the App\Team model;

   public function professionals()  
    {  
      return $this->belongsToMany(  
            Spark::userModel(), 'team_users', 'team_id', 'user_id')  
            ->withPivot('role')  
            ->where('role', 'professional');  
    }

Please or to participate in this conversation.