where() public méthode

Add a basic where clause to the query.
public where ( string | array | Closure $column, string $operator = null, mixed $value = null, string $boolean = 'and' )
$column string | array | Closure
$operator string
$value mixed
$boolean string
 /**
  * {@inheritdoc}
  */
 public function where(Builder $query, $value, $operator = '=')
 {
     if ($value === self::NULL_VALUE) {
         $value = null;
     }
     return $query->where($this->initQuery($query), $operator, $value);
 }
Exemple #2
1
 /**
  * @param \Illuminate\Database\Query\Builder $query
  * @param string                             $type
  *
  * @return \Illuminate\Database\Query\Builder
  * @throws \Exception
  */
 public function scopeOfType($query, $type = 'image')
 {
     if (!in_array($type, ['plain', 'image', 'audio', 'video', 'application'])) {
         throw new \Exception();
     }
     return $query->where('mime', 'like', $type . '/%');
 }
 /**
  * Find by conditions in request
  *
  * @param Builder $query
  * @param array|null $request
  * @return \Illuminate\Database\Query\Builder
  */
 public function scopeFindByRequest($query, $request = NULL)
 {
     if (is_null($request)) {
         $request = Input::all();
     }
     $findable = isset($this->findable) ? $this->findable : [];
     foreach ($request as $field => $value) {
         if (!in_array($field, $findable)) {
             continue;
         }
         if ($field == 'tag') {
             if (isset($request['tag_search']) && $request['tag_search'] == 'any') {
                 $query->withAnyTag($value);
             } else {
                 $query->withAllTags($value);
             }
             continue;
         }
         if (is_array($value)) {
             $query->whereIn($field, $value);
         } elseif (is_scalar($value)) {
             $query->where($field, '=', $value);
         }
     }
 }
Exemple #4
1
 /**
  * Eloquent Scope for tag categories.
  * 
  * @param \Illuminate\Database\Query\Builder $query
  * @param string                             $category
  *
  * @return \Illuminate\Database\Query\Builder
  */
 public function scopeCategory($query, $category)
 {
     if (!is_null($category)) {
         return $query->where('category', '=', $category);
     }
     return $query;
 }
 public function execute(Builder $query)
 {
     $isWhere = false;
     $count = 0;
     foreach ($this->getValuesIterator() as $where) {
         $operator = $where->getOperator();
         if (array_key_exists($operator, WhereValidator::$especial_operator)) {
             $method = $where->getOperatorValue();
             switch ($method) {
                 case WhereValidator::$especial_operator['isNull']:
                 case WhereValidator::$especial_operator['isNotNull']:
                     $query->{$method}($where->getName());
                     break;
                 case WhereValidator::$especial_operator['between']:
                 case WhereValidator::$especial_operator['in']:
                     $query->{$method}($where->getName(), $where->getValue());
                     break;
             }
             continue;
         } else {
             if ($where->getOperatorValue() == WhereValidator::$operator['like']) {
                 $value = '%' . $where->getValue() . '%';
             } else {
                 $value = $where->getValue();
             }
         }
         $query->where($where->getName(), $where->getOperatorValue(), $value);
     }
     return $query;
 }
 /**
  * Constrain a query to an ability for a specific model.
  *
  * @param  \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder  $query
  * @param  \Illuminate\Database\Eloquent\Model  $model
  * @param  bool  $strict
  * @return void
  */
 protected function constrainByModel($query, Model $model, $strict = false)
 {
     $query->where(function ($query) use($model, $strict) {
         $query->where($this->table . '.entity_type', $model->getMorphClass());
         $query->where($this->abilitySubqueryConstraint($model, $strict));
     });
 }
Exemple #7
0
 /**
  * Find a model by its primary key.
  *
  * @param  mixed  $id
  * @param  array  $columns
  * @return \Illuminate\Database\Eloquent\Model|static|null
  */
 public function find($id, $columns = array('*'))
 {
     if (is_array($id)) {
         return $this->findMany($id, $columns);
     }
     $this->query->where($this->model->getQualifiedKeyName(), '=', $id);
     return $this->first($columns);
 }
 /**
  * @test
  */
 public function it_load_events_for_aggregate()
 {
     $aggregateRootId = new BarId('BarId');
     $this->db->table('events')->willReturn($this->queryBuilder);
     $this->queryBuilder->where('aggregate_root_id', 'BarId')->willReturn($this->queryBuilder);
     $this->queryBuilder->chunk(1000, Argument::any())->willReturn(true);
     $this->eventStore->load($aggregateRootId);
 }
 /**
  * Modify a Builder object. Changes here can be nested
  * @param  \Illuminate\Database\Query\Builder $query
  * @return  \Illuminate\Database\Query\Builder $query
  */
 public function apply(\Illuminate\Database\Query\Builder $query)
 {
     //modify $query here
     if ($this->minAge) {
         $query->where('test_users.dob', '<=', Carbon::now()->subYears($this->minAge)->toDateTimeString());
     }
     if ($this->maxAge) {
         $query->where('test_users.dob', '>=', Carbon::now()->subYears($this->maxAge)->toDateTimeString());
     }
     return $query;
 }
 /**
  * Add a "where" clause to the given query.
  *
  * @param  \Illuminate\Database\Query\Builder $query
  * @param  string $key
  * @param  string $extraValue
  * @return void
  */
 protected function addWhere($query, $key, $extraValue)
 {
     if ($extraValue === 'NULL') {
         $query->whereNull($key);
     } elseif ($extraValue === 'NOT_NULL') {
         $query->whereNotNull($key);
     } elseif (Str::startsWith($extraValue, '!')) {
         $query->where($key, '!=', mb_substr($extraValue, 1));
     } else {
         $query->where($key, $extraValue);
     }
 }
Exemple #11
0
 /**
  * Constrain a query to an ability for a specific model.
  *
  * @param  \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder  $query
  * @param  \Illuminate\Database\Eloquent\Model  $model
  * @return void
  */
 public function scopeForModel($query, Model $model)
 {
     $query->where(function ($query) use($model) {
         $query->where('entity_type', $model->getMorphClass());
         $query->where(function ($query) use($model) {
             $query->whereNull('entity_id');
             if ($model->exists) {
                 $query->orWhere('entity_id', $model->getKey());
             }
         });
     });
 }
 /**
  * Call apply() within an "and" block
  * @param \Illuminate\Database\Query\Builder $query
  * @return \Illuminate\Database\Query\Builder $query
  */
 public function applyAnd(\Illuminate\Database\Query\Builder $query)
 {
     $query->where(function ($query) {
         $this->apply($query);
     });
     return $query;
 }
 /**
  * Parse the fulltext search parameter q
  *
  * @param  string $qParam
  * @param  array  $fullTextSearchColumns
  * @return void
  */
 protected function parseFullTextSearch($qParam, $fullTextSearchColumns)
 {
     if ($qParam == '') {
         //Add where that will never be true
         $this->query->whereRaw('0 = 1');
         return;
     }
     $fulltextType = Config::get('apihandler.fulltext');
     if ($fulltextType == 'native') {
         //Use pdo's quote method to be protected against sql-injections.
         //The usual placeholders unfortunately don't seem to work using AGAINST().
         $qParam = $this->query->getConnection()->getPdo()->quote($qParam);
         //Use native fulltext search
         $this->query->whereRaw('MATCH(' . implode(',', $fullTextSearchColumns) . ') AGAINST("' . $qParam . '" IN BOOLEAN MODE)');
         //Add the * to the selects because of the score column
         if (count($this->query->columns) == 0) {
             $this->query->addSelect('*');
         }
         //Add the score column
         $scoreColumn = Config::get('apihandler.fulltext_score_column');
         $this->query->addSelect($this->query->raw('MATCH(' . implode(',', $fullTextSearchColumns) . ') AGAINST("' . $qParam . '" IN BOOLEAN MODE) as `' . $scoreColumn . '`'));
     } else {
         $keywords = explode(' ', $qParam);
         //Use default php implementation
         $this->query->where(function ($query) use($fullTextSearchColumns, $keywords) {
             foreach ($fullTextSearchColumns as $column) {
                 foreach ($keywords as $keyword) {
                     $query->orWhere($column, 'LIKE', '%' . $keyword . '%');
                 }
             }
         });
     }
 }
Exemple #14
0
 /**
  * Метод фильтрует данные по словам
  *
  * @param string|int $search
  * @param null       $value       word
  * @param string     $delimiter   equal sign - '=', 'like' ...
  * @param string     $beforeValue First sign before value
  * @param string     $afterValue  Last sign after value
  */
 protected function filterSearch($search, $value = null, $delimiter = '=', $beforeValue = '', $afterValue = '')
 {
     if ($search) {
         $value = trim($value);
         $search = trim($search);
         /** @var Model $model */
         $model = $this->_query->getModel();
         // поиск по словам
         if (is_string($search) || is_numeric($search)) {
             $this->_query->where(function ($query) use($model, $search, $value, $delimiter, $beforeValue, $afterValue) {
                 /** @var \Illuminate\Database\Eloquent\Builder $query */
                 $tableName = $model->getTable();
                 if ($value) {
                     if (Model::hasColumn($model, $search)) {
                         $query->orWhere($tableName . '.' . $search, $delimiter, $beforeValue . $value . $afterValue);
                     }
                 } else {
                     foreach (\Schema::getColumnListing($tableName) as $column) {
                         // из-за проблем поиска с кириллицей, костыль.
                         if (!preg_match("/[\\w]+/i", $search)) {
                             if (FilterEnum::hasFilterExecuteForCyrillicColumn($column)) {
                                 continue;
                             }
                         }
                         if (Model::hasColumn($model, $column)) {
                             $query->orWhere($tableName . '.' . $column, 'like', '%' . $search . '%');
                         }
                     }
                 }
             });
         }
     }
 }
Exemple #15
0
 /**
  * like wheres function ,call by internal
  * @param array $conds
  * @return $this
  */
 protected function _wheres($conds)
 {
     foreach ($conds as $field => $opAndVal) {
         if (is_null($opAndVal)) {
             $opAndVal = [null];
         }
         $opAndVal = (array) $opAndVal;
         $op = strtolower(count($opAndVal) == 1 ? '=' : $opAndVal[0]);
         $val = last($opAndVal);
         $field = str_contains($field, '.') ? $field : $this->table . '.' . $field;
         switch ($op) {
             case 'in':
                 if (count($val) == 1) {
                     $this->operator->where($field, '=', $val[0]);
                 } else {
                     $this->operator->whereIn($field, $val);
                 }
                 break;
             case 'between':
                 $this->operator->whereBetween($field, $val);
                 break;
             case 'raw':
                 $this->operator->whereRaw($val);
                 break;
             default:
                 $this->operator->where($field, $op, $val);
         }
     }
     return $this;
 }
 /**
  * @param Builder $query
  * @param int     $instanceId
  *
  * @return Builder
  */
 public function scopeInstanceId($query, $instanceId)
 {
     if (!empty($instanceId)) {
         return $query->where('instance_id', '=', $instanceId);
     }
     return $query;
 }
Exemple #17
0
 /**
  * Filters a query object
  *
  * @param \Illuminate\Database\Query\Builder	$query
  * @param array									$selects
  *
  * @return void
  */
 public function filterQuery(QueryBuilder &$query, &$selects = null)
 {
     //if the field isn't empty
     if ($this->getOption('value') !== '') {
         $query->where($this->config->getDataModel()->getTable() . '.' . $this->getOption('field_name'), '=', $this->getOption('value'));
     }
 }
 /**
  * 当前属性以特定字符串结尾
  * @param $attribute
  * @param string|null $value
  * @return static
  */
 public function whereEndsWith($attribute, string $value)
 {
     if (is_empty_string($value)) {
         return $this;
     }
     $this->original->where($attribute, 'like', '%' . trim($value, '%'));
     return $this;
 }
Exemple #19
0
 /**
  * Add a date constraint to the query object
  *
  * @param $field
  * @param ValueExpression $valueExpression
  **/
 protected function parseDate($field, ValueExpression $valueExpression)
 {
     // check if we already have a snake case field name
     if (strpos($field, '_') <= 0) {
         $field = snake_case($field);
     }
     $this->query->where($field, $valueExpression->getExpression(), $valueExpression->getValue()->toDateTimeString());
 }
 /**
  * Filters a query object
  *
  * @param \Illuminate\Database\Query\Builder	$query
  * @param array									$selects
  *
  * @return void
  */
 public function filterQuery(QueryBuilder &$query, &$selects = null)
 {
     $model = $this->config->getDataModel();
     //try to read the time for the min and max values, and if they check out, set the where
     if ($minValue = $this->getOption('min_value')) {
         $time = new \DateTime($minValue);
         if ($time !== false) {
             $query->where($model->getTable() . '.' . $this->getOption('field_name'), '>=', $this->getDateString($time));
         }
     }
     if ($maxValue = $this->getOption('max_value')) {
         $time = new \DateTime($maxValue);
         if ($time !== false) {
             $query->where($model->getTable() . '.' . $this->getOption('field_name'), '<=', $this->getDateString($time));
         }
     }
 }
Exemple #21
0
 /**
  * Get things tagged with all of a given comma-separated list of tags.
  * @param \Illuminate\Database\Query\Builder $query
  * @param string $tagged
  */
 public function scopeTagged($query, $tagged)
 {
     foreach (explode(',', $tagged) as $tag) {
         $query->whereHas('tags', function ($query) use($tag) {
             $query->where('name', $tag);
         });
     }
     return $query;
 }
 /**
  * Setup wildcard query string filter to eloquent or query builder.
  *
  * @param  \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder  $query
  * @param  mixed  $keyword
  * @param  array  $fields
  *
  * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
  */
 protected function setupWildcardQueryFilter($query, $keyword, array $fields)
 {
     if (!empty($keyword) && !empty($fields)) {
         $query->where(function ($query) use($fields, $keyword) {
             $this->buildWildcardQueryFilters($query, $fields, Str::searchable($keyword));
         });
     }
     return $query;
 }
 /**
  * @param  \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query
  *
  * @return void
  */
 public function apply($query)
 {
     $query->where(function ($query) {
         foreach (['where', 'orWhere', 'whereIn', 'orWhereIn'] as $method) {
             foreach ($this->{$method . 's'} as $params) {
                 call_user_func_array([$query, $method], $params);
             }
         }
     });
 }
 /**
  * Add a "where" clause to the given query.
  *
  * @param  \Illuminate\Database\Query\Builder $query
  * @param  string $key
  * @param  string $extraValue
  * @return void
  */
 protected function addWhere($query, $key, $extraValue)
 {
     if ($extraValue === 'NULL') {
         $query->whereNull($key);
     } elseif ($extraValue === 'NOT_NULL') {
         $query->whereNotNull($key);
     } else {
         $query->where($key, $extraValue);
     }
 }
 /**
  * @param string $sku
  * @param int    $units
  * @param int    $subunits
  *
  * @return bool
  */
 public function setPriceBySku(string $sku, int $units, int $subunits)
 {
     /** @var Product $product */
     $product = $this->productResource->where('sku', $sku)->with('prices')->limit(1)->first();
     $price = $product->prices()->firstOrNew([]);
     $price->setAttribute('units', $units);
     $price->setAttribute('subunits', $subunits);
     $price->setAttribute('currency', 'GBP');
     return $price->save();
 }
 /**
  * @param  \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query
  *
  * @return void
  */
 public function apply($query)
 {
     $method = $this->logic === static::LOGIC_AND ? 'where' : 'orWhere';
     $query->where(function ($query) use($method) {
         $value = '%' . str_replace(' ', '%', $this->search) . '%';
         foreach ($this->columns as $column) {
             $query->{$method}($column, 'like', $value);
         }
     });
 }
Exemple #27
0
 protected function process()
 {
     $this->method = 'GET';
     //database save
     switch ($this->action) {
         case "search":
             // prepare the WHERE clause
             foreach ($this->fields as $field) {
                 $field->getValue();
                 //$field->getNewValue();
                 //die( '..'.$field->value);
                 if ($field->value != "") {
                     if (strpos($field->name, "_copy") > 0) {
                         $name = substr($field->db_name, 0, strpos($field->db_name, "_copy"));
                     } else {
                         $name = $field->db_name;
                     }
                     $value = $field->value;
                     switch ($field->clause) {
                         case "like":
                             $this->query = $this->query->where($name, 'LIKE', '%' . $value . '%');
                             break;
                         case "orlike":
                             $this->query = $this->query->orWhere($name, 'LIKE', '%' . $value . '%');
                             break;
                         case "where":
                             $this->query = $this->query->where($name, $field->operator, $value);
                             break;
                         case "orwhere":
                             $this->query = $this->query->orWhere($name, $field->operator, $value);
                             break;
                     }
                 }
             }
         case "reset":
             $this->process_status = "show";
             return true;
             break;
         default:
             return false;
     }
 }
Exemple #28
0
 /**
  * Add a basic where clause to the query.
  *
  * @param  string  $column
  * @param  string  $operator
  * @param  mixed   $value
  * @param  string  $boolean
  * @return \Illuminate\Database\Eloquent\Builder|static
  */
 public function where($column, $operator = null, $value = null, $boolean = 'and')
 {
     if ($column instanceof Closure) {
         $query = $this->model->newQuery();
         call_user_func($column, $query);
         $this->query->addNestedWhereQuery($query->getQuery(), $boolean);
     } else {
         $this->query->where($column, $operator, $value, $boolean);
     }
     return $this;
 }
Exemple #29
0
 /**
  * @param $search
  * @param $columns
  * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query
  * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
  */
 protected function search($search, $columns, $query)
 {
     if ($search) {
         $query->where(function ($query) use($search, $columns) {
             foreach ($columns as $column) {
                 $query->orWhere($column, 'LIKE', "%{$search}%");
             }
         });
     }
     return $query;
 }
 /**
  * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query
  *
  * @return void
  */
 public function apply($query)
 {
     $query->where(function ($query) {
         $logic = $this->logic === static::LOGIC_AND ? 'and' : 'or';
         foreach ($this->criteria as $criteria) {
             $query->whereNested(function ($query) use($criteria) {
                 $criteria->apply($query);
             }, $logic);
         }
     });
 }