diff --git a/src/Defender/Traits/Users/HasRoles.php b/src/Defender/Traits/Users/HasRoles.php index d08976d..2ee7946 100644 --- a/src/Defender/Traits/Users/HasRoles.php +++ b/src/Defender/Traits/Users/HasRoles.php @@ -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); + }); + } }