Skip to content

Commit

Permalink
Merge pull request #1017 from mo7amed-3bdalla7/master
Browse files Browse the repository at this point in the history
Add custom guard query to role scope
  • Loading branch information
drbyte authored Feb 4, 2019
2 parents a805a6f + b9b97c6 commit 447de23
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function roles(): MorphToMany
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeRole(Builder $query, $roles): Builder
public function scopeRole(Builder $query, $roles, $guard = null): Builder
{
if ($roles instanceof Collection) {
$roles = $roles->all();
Expand All @@ -66,14 +66,15 @@ public function scopeRole(Builder $query, $roles): Builder
$roles = [$roles];
}

$roles = array_map(function ($role) {
$roles = array_map(function ($role) use ($guard) {
if ($role instanceof Role) {
return $role;
}

$method = is_numeric($role) ? 'findById' : 'findByName';
$guard = $guard ?: $this->getDefaultGuardName();

return $this->getRoleClass()->{$method}($role, $this->getDefaultGuardName());
return $this->getRoleClass()->{$method}($role, $guard);
}, $roles);

return $query->whereHas('roles', function ($query) use ($roles) {
Expand Down

0 comments on commit 447de23

Please sign in to comment.