public function scopeRole($query, $role, $column = null)
    {
        if (is_null($role)) {
            return $query;
        }

        return $query->whereHas('roles', function ($query) use ($role, $column) {
            if (is_array($role)) {
                $queryColumn = !is_null($column) ? $column : 'roles.slug';

                $query->whereIn($queryColumn, $role);
            } else {
                $queryColumn = !is_null($column) ? $column : (is_numeric($role) ? 'roles.id' : 'roles.slug');

                $query->where($queryColumn, $role);
            }
        });
    }
public function scopeRole($query, $role, $column = null)
    {
        if (is_null($role)) {
            return $query;
        }

        return $query->whereHas('roles', function ($query) use ($role, $column) {
            if (is_array($role)) {
                $queryColumn = !is_null($column) ? $column : 'roles.slug';

                $query->whereIn($queryColumn, $role);
            } else {
                $queryColumn = !is_null($column) ? $column : (is_numeric($role) ? 'roles.id' : 'roles.slug');

                $query->where($queryColumn, $role);
            }
        });
    }
public function scopeRole ( $ query , $ role , $ column = null ) { if ( is_null ( $ role ) ) { return $ query ; } return $ query -> whereHas ( 'roles' , function ( $ query ) use ( $ role , $ column ) { if ( is_array ( $ role ) ) { $ queryColumn = ! is_null ( $ column ) ? $ column : 'roles.slug' ; $ query -> whereIn ( $ queryColumn , $ role ) ; } else { $ queryColumn = ! is_null ( $ column ) ? $ column : ( is_numeric ( $ role ) ? 'roles.id' : 'roles.slug' ) ; $ query -> where ( $ queryColumn , $ role ) ; } } ) ; }
Scope to select users having a specific
role. Role can be an id or slug.

@param \Illuminate\Database\Eloquent\Builder $query
@param int|string $role
@param string $column
@return \Illuminate\Database\Eloquent\Builder
Scope to select users having a specific role . Role can be an id or slug .