From 7785c68e33d134bd35cd4461f56ec1cb28e916ae Mon Sep 17 00:00:00 2001 From: Vinicius Reis Date: Mon, 14 Sep 2015 12:27:33 -0300 Subject: [PATCH] whichRoles scope MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` User::WhichRoles(‘admin’)->get(); User::WhichRoles([‘admin’, ‘agent’])->get(); ``` Issue #47 --- src/Defender/Traits/Users/HasRoles.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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); + }); + } }