/** * 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); } }
/** * 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); } }
/** * 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()); } }); }); }
/** * 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 'notin': if (count($val) == 1) { $this->_operator->where($field, '<>', $val[0]); } else { $this->_operator->whereNotIn($field, $val); } break; case 'between': $this->_operator->whereBetween($field, $val); break; case 'notbetween': $this->_operator->whereNotBetween($field, $val); break; case 'null': if ($val) { $this->_operator->whereNull($field); } else { $this->_operator->whereNotNull($field); } break; case 'raw': $this->_operator->whereRaw($val); break; default: $this->_operator->where($field, $op, $val); } } return $this; }
/** * Add a "where null" clause to the query. * * @param string $column * @param string $boolean * @param bool $not * @return $this * @static */ public static function whereNull($column, $boolean = 'and', $not = false) { return \Illuminate\Database\Query\Builder::whereNull($column, $boolean, $not); }
/** * Constrain a query to an permission for a specific model. * * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $query * @param \Illuminate\Database\Eloquent\Model|string $model * @param bool $strict * @return void */ public function scopeForModel($query, $model, $strict = false) { $model = is_string($model) ? new $model() : $model; $query->where(function ($query) use($model, $strict) { $query->where('entity_type', $model->getMorphClass()); $query->where(function ($query) use($model, $strict) { // If the model does not exist, we want to search for blanket permissions // that cover all instances of this model. If it does exist, we only // want to find blanket permissions if we're not using strict mode. if (!$model->exists || !$strict) { $query->whereNull('entity_id'); } if ($model->exists) { $query->orWhere('entity_id', $model->getKey()); } }); }); }
/** * Parse the remaining filter params * * @param array $filterParams * @return void */ protected function parseFilter($filterParams) { $supportedPostfixes = ['st' => '<', 'gt' => '>', 'min' => '>=', 'max' => '<=', 'lk' => 'LIKE', 'not-lk' => 'NOT LIKE', 'in' => 'IN', 'not-in' => 'NOT IN', 'not' => '!=']; $supportedPrefixesStr = implode('|', $supportedPostfixes); $supportedPostfixesStr = implode('|', array_keys($supportedPostfixes)); foreach ($filterParams as $filterParamKey => $filterParamValue) { $keyMatches = []; //Matches every parameter with an optional prefix and/or postfix //e.g. not-title-lk, title-lk, not-title, title $keyRegex = '/^(?:(' . $supportedPrefixesStr . ')-)?(.*?)(?:-(' . $supportedPostfixesStr . ')|$)/'; preg_match($keyRegex, $filterParamKey, $keyMatches); if (!isset($keyMatches[3])) { if (strtolower(trim($filterParamValue)) == 'null') { $comparator = 'NULL'; } else { $comparator = '='; } } else { if (strtolower(trim($filterParamValue)) == 'null') { $comparator = 'NOT NULL'; } else { $comparator = $supportedPostfixes[$keyMatches[3]]; } } $column = $keyMatches[2]; if ($comparator == 'IN') { $values = explode(',', $filterParamValue); $this->query->whereIn($column, $values); } else { if ($comparator == 'NOT IN') { $values = explode(',', $filterParamValue); $this->query->whereNotIn($column, $values); } else { $values = explode('|', $filterParamValue); if (count($values) > 1) { $this->query->where(function ($query) use($column, $comparator, $values) { foreach ($values as $value) { if ($comparator == 'LIKE' || $comparator == 'NOT LIKE') { $value = preg_replace('/(^\\*|\\*$)/', '%', $value); } //Link the filters with AND of there is a "not" and with OR if there's none if ($comparator == '!=' || $comparator == 'NOT LIKE') { $query->where($column, $comparator, $value); } else { $query->orWhere($column, $comparator, $value); } } }); } else { $value = $values[0]; if ($comparator == 'LIKE' || $comparator == 'NOT LIKE') { $value = preg_replace('/(^\\*|\\*$)/', '%', $value); } if ($comparator == 'NULL' || $comparator == 'NOT NULL') { $this->query->whereNull($column, 'and', $comparator == 'NOT NULL'); } else { $this->query->where($column, $comparator, $value); } } } } } }
private function generateNullConditionFromHasOneOrMany(HasOneOrMany $relation) { $this->query = $this->query->whereNull($relation->getForeignKey()); }
/** * Add where to query builder * @param \Illuminate\Database\Query\Builder $query * @param array $subentity = null only add wheres with this subeneity */ protected function addWhereQuery($query, $subentity = null) { // Ex with both filter and where: select * from `dms` where (`key` like ? or `name` like ?) and `disabled` = ? if (isset($this->where)) { foreach ($this->where as $where) { #$table = $where['table']; $column = $where['column']; $operator = $where['operator']; $value = $where['value']; if ($operator == 'in') { #$query->whereIn("$table.$column", $value); $query->whereIn($column, $value); } elseif ($operator == 'null') { if ($value) { $query->whereNull($column); } else { $query->whereNotNull($column); } } else { #$query->where("$table.$column", $operator, $value); $query->where($column, $operator, $value); } } } }
/** * Parse the remaining filter params * * @param array $filterParams * @return void */ protected function parseFilter($filterParams) { $supportedPostfixes = ['st' => '<', 'gt' => '>', 'min' => '>=', 'max' => '<=', 'lk' => 'LIKE', 'not-lk' => 'NOT LIKE', 'in' => 'IN', 'not-in' => 'NOT IN', 'not' => '!=']; $supportedPrefixesStr = implode('|', $supportedPostfixes); $supportedPostfixesStr = implode('|', array_keys($supportedPostfixes)); foreach ($filterParams as $filterParamKey => $filterParamValue) { $keyMatches = []; //Matches every parameter with an optional prefix and/or postfix //e.g. not-title-lk, title-lk, not-title, title $keyRegex = '/^(?:(' . $supportedPrefixesStr . ')-)?(.*?)(?:-(' . $supportedPostfixesStr . ')|$)/'; preg_match($keyRegex, $filterParamKey, $keyMatches); if (!isset($keyMatches[3])) { if (strtolower(trim($filterParamValue)) == 'null') { $comparator = 'NULL'; } else { $comparator = '='; } } else { if (strtolower(trim($filterParamValue)) == 'null') { $comparator = 'NOT NULL'; } else { $comparator = $supportedPostfixes[$keyMatches[3]]; } } $column = $keyMatches[2]; //resolve a relation->relation_column if we are using an eloquent builder. if (strpos($column, '->') !== false && $this->isEloquentBuilder) { $model = $this->builder->getModel(); //get the relation and the column in the relation $relationComponents = explode('->', $column); if (!method_exists($model, $relationComponents[0])) { throw new ApiHandlerException('UnknownResourceRelation', ['relation' => $relationComponents[0]]); } $relation = call_user_func([$model, $relationComponents[0]]); $relationColumn = $relationComponents[1]; $relationType = $this->getRelationType($relation); if ($relationType === 'BelongsTo') { $firstKey = $relation->getQualifiedForeignKey(); $secondKey = $relation->getQualifiedOtherKeyName(); } else { if ($relationType === 'HasMany' || $relationType === 'HasOne') { $firstKey = $relation->getQualifiedParentKeyName(); $secondKey = $relation->getForeignKey(); } else { if ($relationType === 'BelongsToMany') { $firstKey = $relation->getQualifiedParentKeyName(); $secondKey = $relation->getRelated()->getQualifiedKeyName(); } } } //get the table to join to $joinTable = $relation->getRelated()->getTable(); //do the join $this->query->join($joinTable, $firstKey, '=', $secondKey); //modify the $column name and continue parsing. $column = $joinTable . '.' . $relationColumn; $this->query->addSelect($model->getTable() . '.*'); } //should we be using an eloquent builder, append the table name to every column to differentiate from joined columns. if ($this->isEloquentBuilder) { if (strpos($column, '.') === false) { $column = $this->builder->getModel()->getTable() . '.' . $column; } } if ($comparator == 'IN') { $values = explode(',', $filterParamValue); $this->query->whereIn($column, $values); } else { if ($comparator == 'NOT IN') { $values = explode(',', $filterParamValue); $this->query->whereNotIn($column, $values); } else { $values = explode('|', $filterParamValue); if (count($values) > 1) { $this->query->where(function ($query) use($column, $comparator, $values) { foreach ($values as $value) { if ($comparator == 'LIKE' || $comparator == 'NOT LIKE') { $value = preg_replace('/(^\\*|\\*$)/', '%', $value); } //Link the filters with AND of there is a "not" and with OR if there's none if ($comparator == '!=' || $comparator == 'NOT LIKE') { $query->where($column, $comparator, $value); } else { $query->orWhere($column, $comparator, $value); } } }); } else { $value = $values[0]; if ($comparator == 'LIKE' || $comparator == 'NOT LIKE') { $value = preg_replace('/(^\\*|\\*$)/', '%', $value); } if ($comparator == 'NULL' || $comparator == 'NOT NULL') { $this->query->whereNull($column, 'and', $comparator == 'NOT NULL'); } else { $this->query->where($column, $comparator, $value); } } } } } }
/** * Query scope for only drafted data. * * @param \Illuminate\Database\Query\Builder $query * * @return \Illuminate\Database\Query\Builder */ public function scopeOnlyDrafted($query) { return $query->whereNull(self::PUBLISHED_AT); }
/** * Constrain a query to simple abilities. * * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $query * @return void */ public function scopeSimpleAbility($query) { $query->whereNull("{$this->table}.entity_type"); }
/** * Modify the query to check for available jobs. * * @param \Illuminate\Database\Query\Builder $query * @return void */ protected function isAvailable($query) { $query->where(function ($query) { $query->whereNull('reserved_at'); $query->where('available_at', '<=', $this->getTime()); }); }
public function whereNull($column, $boolean = 'and', $not = false) { $column = is_string($column) ? snake_case($column) : $column; return parent::whereNull($column, $boolean, $not); // TODO: Change the autogenerated stub }
/** * Returns categories that have no parents (top-level categories) * * @param \Illuminate\Database\Query\Builder $query * * @return \Illuminate\Database\Query\Builder $query */ public function scopeTopLevel($query) { return $query->whereNull('parentCategoryId'); }