Skip to content

Commit

Permalink
whichRoles scope
Browse files Browse the repository at this point in the history
```
User::WhichRoles(‘admin’)->get();
User::WhichRoles([‘admin’, ‘agent’])->get();
```
Issue #47
  • Loading branch information
vinicius73 committed Sep 14, 2015
1 parent 5f6b7fa commit 7785c68
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Defender/Traits/Users/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,21 @@ public function syncRoles(array $roles)
{
return $this->roles()->sync($roles);
}

/**
* Take user by roles
*
* @param \Illuminate\Database\Query\Builder $query
* @param string|array $roles
*
* @return \Illuminate\Database\Query\Builder
*/
public function scopeWhichRoles($query, $roles)
{
return $query->whereHas('roles', function($query) use($roles) {
$roles = (is_array($roles)) ? $roles : [$roles];

$query->whereIn('name', $roles);
});
}
}

0 comments on commit 7785c68

Please sign in to comment.