2

I would like to return all users that do not have various relationship existences, and various roles.

At the moment, this works correctly:

User::doesntHave('trusts')
        ->doesntHave('sites')
        ->pluck('email', 'name', 'id');

What I would like is to also remove users that have the 'admin' role.

So far I have tried:

User::doesntHave('trusts')
         ->doesntHave('sites')
         ->whereDoesntHave('roles', function ($query) {
             $query->hasRole(['admin']);
         })
         ->pluck('email', 'name', 'id')

But it's returning the error:

Call to undefined method Illuminate\Database\Query\Builder::hasRole()

How can I filter out the users that have a specific role?

1 Answers1

3

Try this

User::doesntHave('trusts')
         ->doesntHave('sites')
         ->whereDoesntHave('roles', function ($query) {
             $query->where('name', 'admin');
         })
         ->pluck('email', 'name', 'id')
Paras
  • 9,258
  • 31
  • 55