defender icon indicating copy to clipboard operation
defender copied to clipboard

Filter users by role

Open thujohn opened this issue 9 years ago • 3 comments

Hi.

Have you planned to add a method to filter users by role ?

e.g. : User::role('admin')->get() or User::role('guest')->get()

thujohn avatar Jul 03 '15 09:07 thujohn

cool. :+1:

But I think it should have a nicer name, like User::whereRole('admin') or whereRoles(['admin', 'employ']).

Proposal:

1 - whereRoles, whereRole 2 - whichRole, whichRoles 3 - rolesAre(), roleIs()

vluzrmos avatar Jul 03 '15 10:07 vluzrmos

Actually what I did is a simple scope :

public function scopeRole($query, $role)
{
    return $this->whereHas('roles', function($query) use($role){
        $query->where('name', $role);
    });
}

thujohn avatar Jul 03 '15 10:07 thujohn

Need apply tests

vinicius73 avatar Sep 14 '15 15:09 vinicius73