azureus_an's avatar

Get user who doesnt have the specific role from many to many relation

I have users, role and role_user table, how do i get all users who doesnt have this specific role, for example i have role admin, and i want to show the list of users who is not admin so i can add the user to admin role

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

You can use the whereDoesntHave() method to get all users who don't have a specific role.

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

This will return a collection of all users who don't have the role with the name 'admin'.

Please or to participate in this conversation.