0

How to fetch the users list which is not assigned to a particular role?

I have the usual DB structure & relation: users, roles & users_roles with belongsToMany relationship.

For example, role id for 'admin' is 1, how can I fetch users which are not assigned the role with this role id.

I tried the below query but was not successful. It gives me an error that no table was used.

return User::whereNotIn('roles', function($q){
    $q->where('id', 1);
})->get();

P.S: I do not want to check if the user is admin or not, I just want to fetch the users not assigned to a role based on role_id.

Jason Joslin
  • 1,154
  • 8
  • 23
Vaibhav Sidapara
  • 159
  • 2
  • 12

0 Answers0