This scope allows to retrieve the users with a specific permission.
@param  \Illuminate\Database\Eloquent\Builder  $query
@param  string  $permission
@return \Illuminate\Database\Eloquent\Builder
175082
public function scopeWherePermissionIs($query, $permission = '', $boolean = 'and')
    {
        $method = $boolean == 'and' ? 'where' : 'orWhere';

        return $query->$method(function ($query) use ($permission) {
            $query->whereHas('roles.permissions', function ($permissionQuery) use ($permission) {
                $permissionQuery->where('name', $permission);
            })->orWhereHas('permissions', function ($permissionQuery) use ($permission) {
                $permissionQuery->where('name', $permission);
            });
        });
    }
1177983
public function scopeMembers(Builder $query)
    {
        $query->with('roles')->whereNotNull('tbl_users.id')->whereHas('roles', function ($query) {
            $query->whereIn('tbl_roles.name', ['member', 'client']);
        });


        return $query;
    }
979897
protected function doQuery(Query $query)
    {
        $result = static::db()->query($query);

        $this->where = null;

        return $result;
    }
355350
public function findWithPermission($permission)
    {
        return array_filter(
            $this->findAll(),
            function ($user) use ($permission) {
                return $user->has($permission);
            }
        );
    }
1072223
def permitted_query(self, query, group, operations):
        ''''''
        session = query.session
        models = session.router
        user = group.user
        if user.is_superuser:   # super-users have all permissions
            return query
        roles = group.roles.query()
        roles = group.roles.query()  # query on all roles for group
        # The throgh model for Role/Permission relationship
        throgh_model = models.role.permissions.model
        models[throgh_model].filter(role=roles,
                                    permission__model_type=query.model,
                                    permission__operations=operations)

        # query on all relevant permissions
        permissions = router.permission.filter(model_type=query.model,
                                               level=operations)

        owner_query = query.filter(user=user)
        # all roles for the query model with appropriate permission level
        roles = models.role.filter(model_type=query.model, level__ge=level)
        # Now we need groups which have these roles
        groups = Role.groups.throughquery(
            session).filter(role=roles).get_field('group')
        # I need to know if user is in any of these groups
        if user.groups.filter(id=groups).count():
            # it is, lets get the model with permissions less
            # or equal permission level
            permitted = models.instancerole.filter(
                role=roles).get_field('object_id')
            return owner_query.union(model.objects.filter(id=permitted))
        else:
            return owner_query
824984
public function GetByPermission($Permission) {
      if (!is_array($Permission))
         $Permission = array($Permission);
         
      $this->SQL->Select('r.*')
         ->From('Role r')
         ->Join('Permission per', "per.RoleID = r.RoleID")
         ->Where('per.JunctionTable is null');
         
      $this->SQL->BeginWhereGroup();
      $PermissionCount = count($Permission);
      for ($i = 0; $i < $PermissionCount; ++$i) {
         $this->SQL->Where('per.`'.$Permission[$i].'`', 1);
      }
      $this->SQL->EndWhereGroup();
      return $this->SQL->Get();
   }
1040234
public function scopeWhereRatedBy(Builder $query, $userId = null)
    {
        return app(RatingServiceContract::class)
            ->scopeWhereRatedBy($query, $userId);
    }
625515
protected function setKeysForSaveQuery(Builder $query)
    {
        $query->where('tag_id', $this->tag_id)
              ->where('user_id', $this->user_id);

        return $query;
    }
1261239
public function scopeArea(Builder $query, $areas)
    {
        $query->with('roles')->whereNotNull('tbl_users.id');
        if (empty($areas)) {
            return $query;
        }
        $query->whereHas('roles', function ($query) use ($areas) {
            $query->whereIn('tbl_roles.area', (array) $areas);
        });

        return $query;
    }
310950
public function scopeClients(Builder $query)
    {
        $query->with('roles')->whereNotNull('tbl_users.id')->whereHas('roles', function ($query) {
            $query->whereIn('tbl_roles.name', ['member', 'users', 'user']);
        });


        return $query;
    }
520351
protected function getPermission($permission)
    {
        $name = $permission instanceof Permission
            ? $permission->getName()
            : $permission;

        return $this->permissions->filter(function(Permission $current) use ($name) {
            return $current->getName() == $name;
        })->first();
    }
803462
public function preparePermission($permission)
    {
        if ($permission instanceof Permission) {
            return $permission;
        }

        if (is_numeric($permission)) {
            return Permission::whereId($permission)->first();
        }

        return Permission::whereName($permission)->first();
    }
1313429
protected function setKeysForSaveQuery(Builder $query)
    {
        $query->where('discussion_id', $this->discussion_id)
              ->where('user_id', $this->user_id);

        return $query;
    }
142494
public function hasPermission($permission)
    {
        if ($this->isAdmin()) {
            return true;
        }

        if (is_null($this->permissions)) {
            $this->permissions = $this->getPermissions();
        }

        return in_array($permission, $this->permissions);
    }
846644
protected function checkPermission($permission = 'manage', $role = 'admin')
    {
        if ($this->auth->guest()) {
            return 401;
        }

        if ($this->auth->user()->can($permission) || $this->auth->user()->hasRole($role)) {
            return true;
        }

        return 403;
    }
83883
public function scopeForUserContext($query, Context $context)
    {
        if ($context->hasRole('Coach')) {
            return $query->whereHas('member', function ($query) use ($context) {
                /** @noinspection PhpUndefinedMethodInspection */
                $query->where('team_id', $context->team_id);
            });
        }
        return $query->where('inviter_id', $context->id);
    }
853251
public function scopeWithheld(Builder $query)
    {
        return $query->where(function ($query)
        {
            $query->where('status', '<=', Node::DRAFT)
                ->orWhere(function ($query)
                {
                    $query->where('status', '<=', Node::PENDING)
                        ->where('published_at', '>', Carbon::now());
                });
        });
    }