/** * Apply filters in your QueryBuilder based in $fields * * @param \Illuminate\Database\Eloquent\Builder $queryBuilder * @param array $fields * */ public function scopeSearch(Builder $queryBuilder, $fields = []) { if (isset($this->searchable)) { foreach ($fields as $field => $value) { if (array_key_exists($field, $this->searchable)) { switch ($this->searchable[$field]) { // compare equals values case 'MATCH': $queryBuilder->where($field, $value); break; // compare like values // compare like values case 'LIKE': array_map(function ($value) use($queryBuilder, $field) { $queryBuilder->where($field, "LIKE", "%" . $value . "%"); }, explode(" ", $value)); break; // compare between values // compare between values case 'BETWEEN': if (is_array($value) && count($value) == 2) { if (isset($value[0]) && !empty($value[0])) { $queryBuilder->where($field, ">=", $value[0]); } if (isset($value[1]) && !empty($value[1])) { $queryBuilder->where($field, "<=", $value[1]); } } } } } } return $queryBuilder; }
/** * @param \Illuminate\Database\Eloquent\Builder $query * @param int $id * * @return \Illuminate\Database\Eloquent\Builder */ public function scopeEnabled($query, $id = null) { if ($id) { $query->where('id', '=', $id); } return $query->where('disabled', '=', false); }
/** * 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)); }); }
/** * @param Builder $query * @param $wheres */ private function processWhere(Builder $query, $wheres) { $self = $this; foreach ($wheres as $where) { /** @var WhereParameter $where */ $query->where(function (Builder $query) use($self, $where) { if ($comparison = $where->getComparison()) { $query->where($comparison->getSubject(), $comparison->getOperator(), $comparison->getValue()); } foreach ($where->getChildren() as $child) { if ($child instanceof AndConjunction) { $query->where(function (Builder $query) use($self, $child) { $this->processWhere($query, [$child->getSubject()]); }); } elseif ($child instanceof OrConjunction) { $query->orWhere(function (Builder $query) use($self, $child) { $this->processWhere($query, [$child->getSubject()]); }); } else { throw new \InvalidArgumentException("Got an unknown conjunction"); } } }); } }
/** * 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 . '%'); }
public function build(Builder $query) { if (is_array($this->pageId)) { return $query->where('pages.id', 'not in', $this->pageId); } else { return $query->where('pages.id', '!=', $this->pageId); } }
private function searchWithTags(Query $query, stdClass $argument) { return $query->where(function (Query $query) use($argument) { $query->where('unit.name', $argument->operator, $argument->value)->orWhereHas('tags', function (Query $query) use($argument) { $query->where('name', $argument->operator, $argument->value); }); }); }
/** * @inheritdoc */ public function getByCredentials(array $credentials) { foreach ($credentials as $key => $value) { if (strpos($key, 'password') === false) { $this->queryBuilder->where($key, $value); } } return $this->queryBuilder->first(); }
public function scopeActive(Builder $query) { $time = Date::floorToMinute(); return $query->where('disable', '')->where(function (Builder $query) use($time) { return $query->where('start', '')->orWhere('start', '<=', $time); })->where(function (Builder $query) use($time) { return $query->where('stop', '')->orWhere('stop', '>', $time + 60); }); }
public function modifyQuery(Builder $query) { if ($this->get('user_id')) { $query->where('user_id', $this->get('user_id')); } if ($this->get('role_id')) { $query->where('role_id', $this->get('role_id')); } return $query; }
/** * Query scope for finding "similar" slugs, used to determine uniqueness. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $model * @param string $attribute * @param array $config * @param string $slug * @return \Illuminate\Database\Eloquent\Builder */ public function scopeFindSimilarSlugs(Builder $query, Model $model, $attribute, $config, $slug) { $separator = $config['separator']; foreach ($config['uniqueWhere'] as $param) { if ($this->{$param}) { $query->where($param, $this->{$param}); } } return $query->where($attribute, '=', $slug)->orWhere($attribute, 'LIKE', $slug . $separator . '%'); }
/** * Add a where to the query * @param string $field * @param string $operator * @param string $value * @return $this */ public function where($field, $operator = null, $value = null) { // If there is only two arguments, the second one is the value if (func_num_args() == 2) { $value = $operator; $operator = '='; } $this->builder = $this->builder->where($field, $operator, $value); return $this; }
/** * Apply the scope to a given Eloquent query builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @param \Illuminate\Database\Eloquent\Model $model * * @return void */ public function apply(Builder $builder, Model $model) { $strict = isset($model::$strictModeration) ? $model::$strictModeration : config('moderation.strict'); if ($strict) { $builder->where($model->getQualifiedStatusColumn(), '=', Status::APPROVED); } else { $builder->where($model->getQualifiedStatusColumn(), '!=', Status::REJECTED); } $this->extend($builder); }
/** * @param string $sku * @param Request $request * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException * * @return \Illuminate\Http\RedirectResponse */ public function putProductCategory(string $sku, Request $request) { /** @var Product $product */ $product = $this->product->where('sku', '=', $sku)->firstOrFail(); $category = $this->category->findOrFail($request->get('category-id')); $product->category()->associate($category); $product->save(); $this->webUi->successMessage("Set category for `{$product->sku}` to `{$category->name}`."); return $this->webUi->redirect('products.show', [$product->sku]); }
/** * Apply logged in scope. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Gitamin\Models\User $user * * @return \Illuminate\Database\Eloquent\Builder */ public function applyLoggedInScope(Builder $query, User $user) { $isPrivate = function ($query) use($user) { $query->where('visibility_level', '=', self::VISIBILITY_PRIVATE)->where("{$this->tableName}.creator_id", '=', $user->id); }; $whereVisible = function ($query) use($isPrivate) { $query->whereIn('visibility_level', [self::VISIBILITY_PUBLIC, self::VISIBILITY_LOGGED_IN])->orWhere($isPrivate); }; return $query->where($whereVisible); }
/** * Filter dates in query. * * @param string $startDate * @param string $endDate * @return $this */ protected function filterDates($startDate = null, $endDate = null) { if (is_null($startDate) && !is_null($endDate)) { $this->query->where($this->gridQuery->dateReferenceColumn(), '<=', $endDate); } if (!is_null($startDate) && !is_null($endDate)) { $this->query->whereBetween($this->gridQuery->dateReferenceColumn(), [$startDate, $endDate]); } return $this; }
/** * Scope for choosing by date * * @param Builder $query * @param timestamp $until * @param timestamp|null $from * @return Builder */ public function scopeOlderThanOrBetween(Builder $query, $until = null, $from = null) { if (is_null($until)) { $until = Carbon::now(); } $query->where('created_at', '<', $until); if (!is_null($from)) { $query->where('created_at', '>=', $from); } return $query; }
public function modifyQuery(Builder $query) { $query->join('users', 'users.id', '=', 'team_users.user_id'); if ($this->get('team_id')) { $query->where('team_id', $this->get('team_id')); } if ($this->get('user_id')) { $query->where('user_id', $this->get('user_id')); } return $query; }
/** * @param Builder $query * * @return mixed */ public function scopePublished($query) { return $query->where(function ($query) { $query->where(function ($query) { $query->where('published_at', '<=', Carbon::now())->orWhere('published_at', null); }); $query->where(function ($query) { $query->where('concealed_at', '>', Carbon::now())->orWhere('concealed_at', null); }); }); }
/** * @param Builder $builder * @param string $email * @param string $ticket * @return Builder */ public function scopeUnexpired(Builder $builder, $email = null, $ticket = null) { $builder->where('expired_at', '>', Carbon::now()); if (null !== $email) { $builder->where('email', $email); } if (null !== $ticket) { $builder->where('ticket', $ticket); } return $builder; }
/** * This scope eager loads the translations for the default and the fallback locale only. * We can use this as a shortcut to improve performance in our application. * * @param Builder $query */ public function scopeWithTranslation(Builder $query) { $query->with(['translations' => function ($query) { $query->where(function ($query) { $query->where($this->getTranslationsTable() . '.' . $this->getLocaleKey(), $this->locale()); if ($this->useFallback()) { return $query->orWhere($this->getTranslationsTable() . '.' . $this->getLocaleKey(), $this->getFallbackLocale($this->locale()))->orWhere($this->getTranslationsTable() . '.' . $this->getLocaleKey(), $this->getFallbackLocale()); } }); }]); }
/** * Add the qualifying where statement to the query. * * @param string $finder The column to look in * @param array|string $finder The comparison equality * @return Parser The parser instance itself */ public function qualify($finder, $qualifier) { if (is_array($qualifier)) { $equality = array_shift($qualifier); $qualifier = array_shift($qualifier); } else { $equality = '='; } $this->query->where($finder, $equality, $qualifier); return $this; }
/** * Aplicar estrutura de busca em uma BuilderQuery. * * @param Builder $query * @param array $groups */ public function applyQuery(Builder $query, array $groups) { $query->where(function ($query) use($groups) { foreach ($groups as $items) { $query->orWhere(function ($query) use($items) { foreach ($items as $item) { $query->where($item->field, $item->op, $item->value, $item->link); } }); } }); }
/** * Handle the filter query. * * @param Builder $query * @param FilterInterface $filter */ public function handle(Builder $query, FilterInterface $filter) { if ($filter->getValue() == 'live') { $query->where('enabled', true)->where('publish_at', '<', time()); } if ($filter->getValue() == 'scheduled') { $query->where('enabled', true)->where('publish_at', '>', time()); } if ($filter->getValue() == 'draft') { $query->where('enabled', false); } }
/** * Get models that have online non empty translation. * * @param Builder $query * * @return Builder $query */ public function scopeOnline(Builder $query) { if (method_exists($this, 'translations')) { if (!Request::input('preview')) { $query->where('status', 1); } $query->where('locale', config('app.locale')); return $query; } else { return $query->where('status', 1); } }
/** * Fired just before querying. * * @param Builder $query */ public function onQuerying(Builder $query) { $disk = $this->getDisk(); // Limit results to the desired disk. $query->where('disk_id', $disk->getId()); // Limit results to the desired folder if any. if ($folder = $this->getFolder()) { $query->where('folder_id', $folder->getId()); } else { $query->where('folder_id', null); } }
/** * Handle the filter query. * * @param Builder $query * @param FilterInterface $filter */ public function handle(Builder $query, FilterInterface $filter) { if ($filter->getValue() == 'active') { $query->where('enabled', true)->where('activated', true); } if ($filter->getValue() == 'inactive') { $query->where('enabled', true)->where('activated', false); } if ($filter->getValue() == 'disabled') { $query->where('enabled', false); } }
/** * Fired just before querying. * * @param Builder $query */ public function onQuerying(Builder $query) { $disk = $this->getDisk(); // Limit results to the desired disk. $query->where('disk_id', $disk->getId()); // Limit results to the desired parent folder if any. if ($parent = $this->getParent()) { $query->where('parent_id', $parent->getId()); } else { $query->where('parent_id', null); } }
/** * 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()); } }); }); }
/** * @param Builder $query * @param $column * @param $value */ private function query(Builder $query, $column, $value) { if (is_array($value)) { if (isset($value['from'])) { $query->where($column, '>=', $value['from']); } if (isset($value['to'])) { $query->where($column, '<=', $value['to']); } } else { $query->where($column, 'like', "%{$value}%"); } }