Laravel-permission: Users where role = ?

Created on 26 Oct 2016  路  10Comments  路  Source: spatie/laravel-permission

First of all, amazing package. Super simple to use!

I guess in theory you can hit the Roles table and grab all users where role = x

But it would be nice to have a function that does something like, User::whereRole('trial user')->all() for example.

Most helpful comment

User::role('trial user')->get();

All 10 comments

I was just about to post about how my theory failed and had another idea. So for people looking to get around the same issue, with the feature not implemented, here's what you can do:

$role = Role::where('name', 'admin')->first(); $usersOfRole = $role->users()->get();

Eloquent has you covered with the whereHas method. It can be used to query relations of a model.

This is how you use it:

User::whereHas('role', function(Builder $query) {
   return $query->name === 'trial user';
});

Disclaimer: untested code 馃槃 If it doesn't work, just search the Laravel docs for whereHas.

Just to leave a trace, if anyone is looking for this, I couldn't make the code above from @freekmurze work, so I went on and checked the Laravel docs ;-)

This would work:

User::whereHas('roles', function ($query) {
    return $query->where('name', 'name-of-role');
});

What I changed was "role" to "roles" as the relationship on the hasRoles trait uses the word in plural, and I couldn't query the "name" field directly, had to search for it.

My use case was that I needed to find the person holding the role "owner" on a certain model, so I went on and created an accessor on said model:

public function getOwnerAttribute()
    {
        return $this->users()->whereHas('roles', function ($query) {
            return $query->where('name', 'owner');
        })->first();
    }

So also, thanks Freek, I actually never had a use case for whereHas before, now I'll probably use it everywhere I can! :D

User::role('trial user')->get();

How do I make this work with or? This does not work User::role('admin|moderator')->get();

I figured out the answer to my own question ... here is what I did in case anybody else needs it.

$operator_admins = User::role('operator_admin')->orderBy('name')->pluck('name', 'id');
$operator_accountants = User::role('operator_accountant')->orderBy('name')->pluck('name', 'id');
$operator_technicians = User::role('operator_technician')->orderBy('name')->pluck('name', 'id');
$manager_select = $operator_admins->union($operator_accountants)->union($operator_technicians);

You could try User::hasAnyRole(['operator_admin','operator_accountant','operator_technician'])->get();

@dweb-x I tried your code - throws error Non-static method App\User::hasAnyRole() should not be called statically

The User::role() scope allows you to pass a role name as a string, or an array of role-name-strings, or a collection of role objects.

Thus User::role(['role1', 'role2', 'role3'])->get() should work, as would User::role($collectionOfRoleObjects)->get()

https://github.com/spatie/laravel-permission/blob/3dbab1c2b2bf721d5ff25b6435ad3f2ff7d45e42/src/Traits/HasRoles.php#L51-L88

@drbyte yessir ... that worked

$manager_select = User::role(['operator_admin','operator_accountant','operator_technician'])->orderBy('name')->pluck('name', 'id');
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

younus93 picture younus93  路  4Comments

notflip picture notflip  路  3Comments

bhulsman picture bhulsman  路  3Comments

feliperoan picture feliperoan  路  3Comments