Search code examples
phplaravellaravel-5eloquententrust

Laravel Entrust, query where users do not have a role


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?


Solution

  • Try this

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