Constrain the given query by the provided role.

@param  \Illuminate\Database\Eloquent\Builder  $query
@param  string  $role
@return \Illuminate\Database\Eloquent\Builder
170220
public function constrainWhereIsNot($query, $role)
    {
        $roles = array_slice(func_get_args(), 1);

        return $query->whereDoesntHave('roles', function ($query) use ($roles) {
            $query->whereIn('name', $roles);
        });
    }
1202380
public function limit($query)
    {
        $clause = '';

        if (!empty($query->limit)) {
            switch ($query->operation['type']) {
                case AnDomainQuery::QUERY_SELECT_DEFAULT :
                case AnDomainQuery::QUERY_SELECT  :
                    $clause = ' LIMIT '.$query->offset.' , '.$query->limit;
                    break;
                case AnDomainQuery::QUERY_UPDATE :
                    if ((int) $query->limit > 0) {
                        $clause = ' LIMIT '.$query->limit;
                    }
                    break;
            }
        }

        return $clause;
    }
1196759
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        if ($parentQuery->getQuery()->from === $query->getQuery()->from) {
            return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns);
        }

        return $query->select($columns)->whereColumn(
            $this->getQualifiedForeignKey(),
            '=',
            $query->getModel()->getTable() . '.' . $this->ownerKey
        );
    }
1369672
protected function compileRowConstraint($query)
    {
        $start = $query->offset + 1;

        if ($query->limit > 0) {
            $finish = $query->offset + $query->limit;

            return "between {$start} and {$finish}";
        }

        return ">= {$start}";
    }
147317
public function addRole(Role $role)
    {
        if (!in_array($role, $this->roles)) {
            $this->roles[] = $role;
            return true;
        }
        return false;
    }
866217
protected function setKeysForSaveQuery(Builder $query)
    {
        $query->where($this->foreignKey, $this->getAttribute($this->foreignKey));

        return $query->where($this->relatedKey, $this->getAttribute($this->relatedKey));
    }
557541
public function setRole( Acl\Role\RoleInterface $role = null )
    {
        $this->role = $role;
        return $this;
    }
786439
public function scopeWhereRoleIs($query, $role = '', $team = null, $boolean = 'and')
    {
        $method = $boolean == 'and' ? 'whereHas' : 'orWhereHas';

        return $query->$method('roles', function ($roleQuery) use ($role, $team) {
            $teamsStrictCheck = Config::get('laratrust.teams_strict_check');
            $roleQuery->where('name', $role)
                ->when($team || $teamsStrictCheck, function ($query) use ($team) {
                    $team = Helper::getIdFor($team, 'team');
                    return $query->where(Helper::teamForeignKey(), $team);
                });
        });
    }
1053786
protected function performJoin(Builder $query = null)
    {
        $query = $query ?: $this->query;

        $ownerKey = $this->getQualifiedOwnerKeyName();

        $query->join($this->throughChild->getTable(), $ownerKey , '=', $this->getQualifiedFirstKeyName());
        $query->select([
            '*' => $this->related->qualifyColumn('*'),
            $this->secondKey => $this->throughChild->getQualifiedKeyName().' as '.$this->secondKey
        ]);

        if ($this->throughChildSoftDeletes()) {
            $query->whereNull($this->throughChild->getQualifiedDeletedAtColumn());
        }
    }
814170
protected function performJoin(Builder $query = null)
    {
        $query = $query ?: $this->query;

        $throughParents = array_reverse($this->throughParents);
        $foreignKeys = array_reverse($this->foreignKeys);
        $localKeys = array_reverse($this->localKeys);

        $segments = explode(' as ', $query->getQuery()->from);

        $alias = $segments[1] ?? null;

        foreach ($throughParents as $i => $throughParent) {
            $predecessor = $throughParents[$i - 1] ?? $this->related;

            $prefix = $i === 0 && $alias ? $alias.'.' : '';

            $this->joinThroughParent($query, $throughParent, $predecessor, $foreignKeys[$i], $localKeys[$i], $prefix);
        }
    }
716708
protected function setKeysForSaveQuery(Builder $query)
    {
        $keys = $this->getKeyName();

        if (!is_array($keys)) {
            return parent::setKeysForSaveQuery($query);
        }

        foreach ($keys as $key) {
            $query->where($key, '=', $this->getKeyForSaveQuery($key));
        }

        return $query;
    }
774423
public StructuredQueryDefinition containerConstraint(String constraintName, StructuredQueryDefinition query) {
    checkQuery(query);
    return new ContainerConstraintQuery(constraintName, query);
  }
675504
public function remove(Builder $query, Model $model)
    {
        $query = $query->getQuery();

        $query->wheres = (new Collection($query->wheres))->reject(function ($where) {
            return $this->isScopeConstraint($where);
        })->values()->all();
    }
1114054
protected function setKeysForSaveQuery(Builder $query)
    {
        foreach ($this->getKeyName() as $key) {
            if (isset($this->$key))
                $query->where($key, '=', $this->$key);
            else
                throw new Exception(__METHOD__ . 'Missing part of the primary key: ' . $key);
        }

        return $query;
    }
890556
public function build(Builder $query)
    {
        if ($this->page === null) {
            return $query->whereNull('parent_id');
        }

        list($col, $direction) = $this->page->getChildOrderingPolicy();

        return $query
            ->where('parent_id', '=', $this->page->getId())
            ->orderBy($col, $direction);
    }
981220
public function scopeFiltered(Builder $builder, array $query = [])
    {
        $query = (array)($query ?: Input::all());
        $this->validateFieldNames($query);

        $mode = $this->getQueryMode($query);
        $query = $this->filterNonSearchableParameters($query);
        $constraints = $this->getConstraints($builder, $query);

        $this->applyConstraints($builder, $constraints, $mode);
    }
393952
public function scopeProtectAdmins(Builder $query)
    {
        /** @var  self  $user */
        $user = auth()->user();

        return ($user && $user->is_admin)
            ? $query
            : $query->where('is_admin', false);
    }