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

SalmanMalik's avatar

Query Users according to specific role (Laravel Spatie)

Hello, I have a really simple question and I am stuck on it. How can I get users that have only a specific role? i.e. I want to query users that have an "employee" role only but in general, they can have multiple roles. Example: User 1 - Employee - Accounts User 2 - Employee

In this example, I only want user 2.

0 likes
3 replies
Sinnbeck's avatar
$users = User::role('employee')->get();

If I recall correctly

Sinnbeck's avatar

@SalmanMalik ah ok got it. Try this

$users = User::whereHas('roles', function ($query) {
    $query->where('name', 'employee');
}, 1)->get(); 

Please or to participate in this conversation.