/** * Helper for "LIKE" filter * * @param String $column * @param String $value * @return Builder */ protected function like($column, $value) { if ($this->builder->getQuery()->getConnection()->getDriverName() == 'pgsql') { return $this->builder->where($column, 'ILIKE', '%' . $value . '%'); } return $this->builder->where($column, 'LIKE', '%' . $value . '%'); }
/** * Handle the command. * * @param RoleRepositoryInterface $roles */ public function handle(RoleRepositoryInterface $roles) { if (!$this->query->getQuery()->joins && ($permission = array_get($this->fieldType->getConfig(), 'permission'))) { $accessible = $roles->findByPermission($permission); if (!$accessible->isEmpty()) { $this->query->join('users_users_roles', 'users_users_roles.entry_id', '=', 'users_users.id')->whereIn('users_users_roles.related_id', $accessible->lists('id')); } } }
/** * Unjoin the tables * * @param \Illuminate\Database\Eloquent\Builder $builder * @param \Illuminate\Database\Eloquent\Model|null $model * @return void */ public function remove(Builder $builder, EloquentModel $model = null) { foreach ($this->joins as $join) { $index = array_search($join, $builder->getQuery()->joins, true); if ($index !== false) { unset($builder->getQuery()->joins[$index]); } } }
public function newEloquentBuilder($query) { $builder = new Builder($query); $builder->macro('key', function (Builder $builder) { return $builder->getQuery()->key(); }); $builder->macro('flush', function (Builder $builder) { return $builder->getQuery()->flush(); }); return $builder; }
/** * Add the "has" condition where clause to the query. * * @param \Illuminate\Database\Eloquent\Builder $hasQuery * @param \Illuminate\Database\Eloquent\Relations\Relation $relation * @param string $operator * @param int $count * @param string $boolean * @return \Illuminate\Database\Eloquent\Builder */ protected function addHasWhere(EloquentBuilder $hasQuery, Relation $relation, $operator, $count, $boolean) { $query = $hasQuery->getQuery(); // Get the number of related objects for each possible parent. $relationCount = array_count_values($query->lists($relation->getHasCompareKey())); // Remove unwanted related objects based on the operator and count. $relationCount = array_filter($relationCount, function ($counted) use($count, $operator) { // If we are comparing to 0, we always need all results. if ($count == 0) { return true; } switch ($operator) { case '>=': case '<': return $counted >= $count; case '>': case '<=': return $counted > $count; case '=': case '!=': return $counted == $count; } }); // If the operator is <, <= or !=, we will use whereNotIn. $not = in_array($operator, array('<', '<=', '!=')); // If we are comparing to 0, we need an additional $not flip. if ($count == 0) { $not = !$not; } // All related ids. $relatedIds = array_keys($relationCount); // Add whereIn to the query. return $this->whereIn($this->model->getKeyName(), $relatedIds, $boolean, $not); }
public function scopeWithRelatedTranslations(Builder $query, $relation, $andFields = []) { $model = $relation->getRelated(); $withFallback = $model->usePublicFallback(); $translatedAttributes = array_merge($model->translatedAttributes, $andFields); $translatedAttributes = array_map(function ($attribute) use($model) { return $model->getPublicTranslationsTable() . '.' . $attribute . ' AS ' . $model->getPublicTranslationsTable() . '_' . $attribute; }, $translatedAttributes); $query->withTranslations(); $translatedAttributes = array_merge((array) $query->getQuery()->columns, $translatedAttributes); $query->select($translatedAttributes)->leftJoin($model->getPublicTranslationsTable(), $model->getPublicTranslationsTable() . '.' . $model->getRelationKey(), '=', $relation->getQualifiedForeignKey())->where(function ($query) use($model, $withFallback) { $query->where($model->getPublicTranslationsTable() . '.' . $model->getLocaleKey(), $model->publicLocale()); if ($withFallback) { $query->orWhere(function (Builder $q) use($model) { $q->where($model->getPublicTranslationsTable() . '.' . $model->getLocaleKey(), $model->getPublicFallbackLocale($model->publicLocale()))->whereNotIn($model->getPublicTranslationsTable() . '.' . $model->getRelationKey(), function (QueryBuilder $q) use($model) { $q->select($model->getPublicTranslationsTable() . '.' . $model->getRelationKey())->from($model->getPublicTranslationsTable())->where($model->getPublicTranslationsTable() . '.' . $model->getLocaleKey(), $model->publicLocale()); }); })->orWhere(function (Builder $q) use($model) { $q->where($model->getPublicTranslationsTable() . '.' . $model->getLocaleKey(), $model->getPublicFallbackLocale())->whereNotIn($model->getPublicTranslationsTable() . '.' . $model->getRelationKey(), function (QueryBuilder $q) use($model) { $q->select($model->getPublicTranslationsTable() . '.' . $model->getRelationKey())->from($model->getPublicTranslationsTable())->where($model->getPublicTranslationsTable() . '.' . $model->getLocaleKey(), $model->getPublicFallbackLocale($model->publicLocale()))->orWhere($model->getPublicTranslationsTable() . '.' . $model->getLocaleKey(), $model->publicLocale()); }); }); } }); }
private function checkQuery(Builder $query) { //nobody gets anything if (count($query->getQuery()->wheres) < 1) { return $query->where('id', '<', 1); } }
protected function getArchivedAtColumn(Builder $builder) { if (count($builder->getQuery()->joins) > 0) { return $builder->getModel()->getQualifiedArchivedAtColumn(); } return $builder->getModel()->getArchivedAtColumn(); }
/** * @param Builder $query * @param SortInterface $sort */ public static function sort(Builder $query, SortInterface $sort) { /** @var Order $order */ foreach ($sort->orders() as $propertyName => $order) { $query->getQuery()->orderBy($propertyName, $order->isAscending() ? 'ASC' : 'DESC'); } }
/** * Scope a query to include tickers. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeTickers(Builder $query, $quantity) { $instance = ($combined = $this->combined) ? $query->getQuery()->newQuery()->from($this->getTable()) : $query; $instance = $instance->where('type', 'ticker')->take($quantity); $this->combined = true; return $combined ? $query->unionAll($instance) : $instance; }
/** * Get the siblings of the specified entry ID * * @param \Illuminate\Database\Eloquent\Builder $query * @param int|array $entryId * @return \Illuminate\Database\Eloquent\Builder */ public function scopeSiblings(Builder $query, $entryId) { $entryId = is_array($entryId) ? $entryId : array($entryId); $connection = $query->getQuery()->getConnection(); $tablePrefix = $connection->getTablePrefix(); return $query->join('relationships', 'relationships.child_id', '=', 'channel_titles.entry_id')->join($connection->raw("`{$tablePrefix}relationships` AS `{$tablePrefix}relationships_2`"), 'relationships_2.parent_id', '=', 'relationships.parent_id')->addSelect('*')->addSelect('relationships_2.child_id AS sibling_id')->whereIn('relationships_2.child_id', $entryId)->orderBy('relationships.order', 'asc')->groupBy('relationships_2.child_id')->groupBy('relationships.field_id')->groupBy('channel_titles.entry_id'); }
/** * Add the constraints for a relationship count query. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parent * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationCountQuery(Builder $query, Builder $parent) { if ($parent->getQuery()->from == $query->getQuery()->from) { return $this->getRelationCountQueryForSelfRelation($query, $parent); } return parent::getRelationCountQuery($query, $parent); }
/** * Add the constraints for a relationship query. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parent * @param array|mixed $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*']) { if ($parent->getQuery()->from == $query->getQuery()->from) { return $this->getRelationQueryForSelfRelation($query, $parent, $columns); } return parent::getRelationQuery($query, $parent, $columns); }
/** * Get the "deleted at" column for the builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @return string */ protected function getFeaturedAtColumn(Builder $builder) { if (count($builder->getQuery()->joins) > 0) { return $builder->getModel()->getQualifiedFeaturedAtColumn(); } else { return $builder->getModel()->getFeaturedAtColumn(); } }
/** * Remove the scope from the given Eloquent query builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @param \Illuminate\Database\Eloquent\Model $model * @return void */ public function remove(Builder $builder, Model $model) { $table = $model->getVersionTable(); $query = $builder->getQuery(); $query->joins = collect($query->joins)->reject(function ($join) use($table) { return $this->isVersionJoinConstraint($join, $table); })->values()->all(); }
/** * Remove the scope from the given Eloquent query builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @param \Illuminate\Database\Eloquent\Model $model * * @return void */ public function remove(Builder $builder, Model $model) { $column = $model->getQualifiedDraftColumn(); $query = $builder->getQuery(); $query->wheres = collect($query->wheres)->reject(function ($where) use($column) { return $this->isDraftConstraint($where, $column); })->values()->all(); }
/** * Remove the scope from the given Eloquent query builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @param \Illuminate\Database\Eloquent\Model $model * @return void */ public function remove(Builder $builder, Model $model) { $columns = $model->getGeometries(); $query = $builder->getQuery(); // $query->wheres = collect($query->wheres)->reject(function ($where) use ($column) { // return $this->isSoftDeleteConstraint($where, $column); // })->values()->all(); }
protected function checkQueryGroupBy() { $groups = $this->query->getQuery()->groups; $keyGroup = $this->model->getQualifiedKeyName(); if (empty($groups) || !in_array($keyGroup, $groups)) { $this->query->groupBy($keyGroup); } }
public function excludeCriterion(QueryBuilder $query, $columns) { $columns = static::getArgumentsArray($columns, func_get_args()); // Get the attributes of the model and filter out all but specified $schema = $query->getQuery()->getConnection()->getSchemaBuilder(); $allColumns = $schema->getColumnListing($query->getModel()->getTable()); $difference = array_diff($allColumns, $columns); return $query->select($difference); }
/** * Add the constraints for a relationship count query. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parent * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationCountQuery(Builder $query, Builder $parent) { if ($parent->getQuery()->from == $query->getQuery()->from) { return $this->getRelationCountQueryForSelfRelation($query, $parent); } $query->select(new Expression('count(*)')); $otherKey = $this->wrap($query->getModel()->getTable() . '.' . $this->otherKey); return $query->where($this->getQualifiedForeignKey(), '=', new Expression($otherKey)); }
/** * Remove the scope from the given Eloquent query builder. * * @param Builder $builder * @param Model $post * @return void */ public function remove(Builder $builder, Model $post) { $query = $builder->getQuery(); unset($query->wheres[$this->whereIndex]); $query->wheres = array_values($query->wheres); $whereBindings = $query->getRawBindings()['where']; array_splice($whereBindings, $this->bindingIndex, $this->bindingCount); $query->setBindings(array_values($whereBindings)); }
/** * @param Builder $query */ protected function applySorting(Builder $query) { $fields = $this->fields(); $field = Input::get('sort'); if (!isset($fields[$field])) { return; } $query->getQuery()->orderBy($this->qualifiedFieldName($this->model, $field, true), $this->sortDirection()); }
/** * Remove the scope from the given Eloquent query builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @return void */ public function remove(Builder $builder, Model $model) { $query = $builder->getQuery(); unset($query->wheres[$this->where_index]); $where_bindings = $query->getRawBindings()['where']; unset($where_bindings[$this->binding_index]); $query->setBindings(array_values($where_bindings)); $query->wheres = array_values($query->wheres); }
/** * Remove scope from the query. * * @param \Illuminate\Database\Eloquent\Builder $builder * @param \Illuminate\Database\Eloquent\Model $model * @return void */ public function remove(Builder $builder, Model $model) { $query = $builder->getQuery(); $bindingKey = 0; foreach ((array) $query->wheres as $key => $where) { if (str_is('is_admin', $key)) { $this->removeWhere($query, $key); } } }
/** * Remove the scope from the given Eloquent query builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @param \Illuminate\Database\Eloquent\Model $model * * @return void */ public function remove(Builder $builder, Model $model) { $query = $builder->getQuery(); foreach ((array) $query->wheres as $key => $where) { if ($where['column'] === $model->getTypeKeyName()) { unset($query->wheres[$key]); $query->wheres = array_values($query->wheres); } } }
/** * Gets all the joined tables. * * @return array */ public function getJoinedTables() { $joins = []; if (is_array($queryJoins = $this->query->getQuery()->joins)) { $joins = array_map(function ($join) { return $join->table; }, $queryJoins); } return $joins; }
/** * Remove scope from the query * * @param \Illuminate\Database\Eloquent\Builder $builder * @param \Illuminate\Database\Eloquent\Model $model */ public function remove(Builder $builder, Model $model) { $query = $builder->getQuery(); $column = $model->getQualifiedPositionColumn(); foreach ((array) $query->orders as $key => $order) { if ($order['column'] == $column) { $this->removeOrderBy($query, $key); } } }
/** * {@inheritdoc} */ public function constraintBuilder(EloquentBuilder $builder, array $options) { if ($relations = $this->eagerLoads(array_get($options, 'simple'))) { $builder->with($relations); } $query = $builder->getQuery(); if ($value = array_get($options, 'order', $this->defaultOrder)) { $this->order($query, $value); } }
public function remove(Builder $builder, Model $model) { $query = $builder->getQuery(); foreach ((array) $query->wheres as $key => $where) { if ($this->isTenantConstraint($model, $where, $this->tenant_col, 1)) { unset($query->wheres[$key]); $query->wheres = array_values($query->wheres); break; } } }
/** * @param EloquentBuilder $builder * @param array $input */ public function constraintBuilder(EloquentBuilder $builder, array $input) { $query = $builder->getQuery(); /** @var BaseFilter $filter */ foreach ($this->items as $filter) { $key = $filter->getDataKey(); if (isset($input[$key])) { $filter->applyFilterConstraint($query, $input[$key]); } } }