Constrain a query to having the given name.

@param  \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder  $query
@return string|array  $name
@return bool  $strict
@return void
438055
public function scopeByName($query, $name, $strict = false)
    {
        $names = (array) $name;

        if (! $strict && $name !== '*') {
            $names[] = '*';
        }

        $query->whereIn("{$this->table}.name", $names);
    }
996903
protected function setJoin(Builder $query = null)
    {
        $query = $query ?: $this->query;

        $foreignKey = $this->related->getTable().'.'.$this->related->getKeyName();

        $localKey = $this->parent->getTable().'.'.$this->secondKey;

        $query->join($this->parent->getTable(), $localKey, '=', $foreignKey);
    }
500848
protected static function filterQuery($query)
    {
        if (! is_string($query)) {
            throw new InvalidArgumentException(sprintf(
                'Invalid query provided; must be a string argument',
                (is_object($query) ? get_class($query) : gettype($query))
            ));
        }

        return static::filterQueryOrFragment(ltrim($query, '?'));
    }
794805
function strict(query) {
  return foldl(function(acc, val, key) {
    if (has.call(allowedKeys, key)) acc[key] = val;
    return acc;
  }, {}, utm(query));
}
736046
protected function setKeysForSaveQuery(Builder $query)
    {
        if (count($this->primaryKey)) {
            foreach ($this->primaryKey as $key) {
                if (isset($this->original[$key])) {
                    $value = $this->original[$key];
                } else {
                    $value = $this->getAttribute($key);
                }
                $query->where($key, $value);
            }
        } else {
            $query->where($this->getKeyName(), '=', $this->getKeyForSaveQuery());
        }

        return $query;
    }
959631
public function makeQueryFiltered($query)
    {
        $settingTarget = $this->getSettingTarget();
        if ($settingTarget) {
            $query = $query->where([
                ['settable_id', '=', $settingTarget->getKeyName()],
                ['settable_type', '=', $settingTarget->getMorphClass()]
            ]);
        } else {
            $query = $query->whereNull('settable_id')->whereNull('settable_type');
        }
        return $query;
    }
385206
public function buildQuery($query)
    {
        $q = $this->request->getQuery();

        foreach ($query as $key => $value)
        {
            $q[$key] = $value;
        }
    }
937838
public function parseKey(Query $query, $key, $strict = false)
    {
        if (is_numeric($key)) {
            return $key;
        } elseif ($key instanceof Expression) {
            return $key->getValue();
        }

        $key = trim($key);

        if (strpos($key, '->') && false === strpos($key, '(')) {
            // JSON字段支持
            list($field, $name) = explode('->', $key, 2);

            return 'json_extract(' . $field . ', \'$.' . str_replace('->', '.', $name) . '\')';
        } elseif (strpos($key, '.') && !preg_match('/[,\'\"\(\)`\s]/', $key)) {
            list($table, $key) = explode('.', $key, 2);

            $alias = $query->getOptions('alias');

            if ('__TABLE__' == $table) {
                $table = $query->getOptions('table');
                $table = is_array($table) ? array_shift($table) : $table;
            }

            if (isset($alias[$table])) {
                $table = $alias[$table];
            }
        }

        if ($strict && !preg_match('/^[\w\.\*]+$/', $key)) {
            throw new Exception('not support data:' . $key);
        }

        if ('*' != $key && ($strict || !preg_match('/[,\'\"\*\(\)`.\s]/', $key))) {
            $key = '`' . $key . '`';
        }

        if (isset($table)) {
            if (strpos($table, '.')) {
                $table = str_replace('.', '`.`', $table);
            }

            $key = '`' . $table . '`.' . $key;
        }

        return $key;
    }
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;
    }
760826
protected function setKeysForSaveQuery(Builder $query)
    {
        parent::setKeysForSaveQuery($query);
        $query->where('locale', '=', $this->locale);

        return $query;
    }
292046
public function constrainQuery(EloquentBuilder &$query, $relatedModel, $constraint)
	{
		//if the column hasn't been joined yet, join it
		if (!$this->validator->isJoined($query, $this->getOption('table')))
		{
			$query->join($this->getOption('table'), $relatedModel->getTable().'.'.$relatedModel->getKeyName(), '=', $this->getOption('column2'));
		}

		$query->where($this->getOption('table').'.'.$this->getOption('column'), '=', $constraint);
	}
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;
    }
929963
public function buildQuery($query = null)
    {
        if (null === $query) {
            $query = $this->query;
        }

        $sql = "SELECT ";
        if (isset($query['distinct']) && $query['distinct']) {
            $sql .= 'DISTINCT ';
        }
        $sql .= isset($query['select']) ? $query['select'] : '*';

        if (!isset($query['from'])) {
            return false;
        }

        $sql .= "\nFROM " . $query['from'];
        if (isset($query['join'])) {
            $sql .= "\n" . (is_array($query['join']) ? implode("\n", $query['join']) : $query['join']);
        }
        if (isset($query['where']) && $query['where'] !== '') {
            $sql .= "\nWHERE " . $query['where'];
        }
        if (isset($query['group'])) {
            $sql .= "\nGROUP BY " . $query['group'];
            if (isset($query['having'])) {
                $sql .= "\nHAVING " . $query['having'];
            }
        }
        if (isset($query['order'])) {
            $sql .= "\n ORDER BY " . $query['order'];
        }
        $limit = isset($query['limit']) ? $query['limit'] : 0;
        $offset = isset($query['offset']) ? $query['offset'] : 0;
        $sql = $this->db->buildLimitOffset($sql, $limit, $offset);

        if (isset($query['union'])) {
            $sql .= "\n" . (is_array($query['union']) ? implode("\n", $query['union']) : $query['union']);
        }

        return $sql;
    }
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;
    }
967827
public function parseKey(Query $query, $key, $strict = false)
    {
        return $key instanceof Expression ? $key->getValue() : $key;
    }
1374911
public function setQuery($query)
    {
        if (!($query instanceof QueryString)) {
            $this->query = new QueryString($query);

            return $this;
        }

        $this->query = $query;

        return $this;
    }
1086975
public function constrainWhereIsAll($query, $role)
    {
        $roles = array_slice(func_get_args(), 1);

        return $query->whereHas('roles', function ($query) use ($roles) {
            $query->whereIn('name', $roles);
        }, '=', count($roles));
    }