public static function scopeWithTag(Builder $query, $tags, $type = 'slug') { $tags = (new static())->prepareTags($tags); return $query->whereHas('tags', function ($query) use($type, $tags) { $query->whereIn($type, $tags); }); }
/** * Add a whereIn to the query * * @param string $field * @param array $values * @param bool $order * * @return $this */ public function whereIn($field, $values, $order = false) { $this->builder = $this->builder->whereIn($field, $values); if ($order) { $this->builder = $this->builder->orderByRaw(DB::raw("FIELD(" . $field . ", " . implode(',', $values) . ")")); } return $this; }
public function scopeInLog(Builder $query, ...$logNames) : Builder { if (is_array($logNames[0])) { $logNames = $logNames[0]; } return $query->whereIn('log_name', $logNames); }
/** * In * * @param $key * @param $value * @return $this */ private function setIn($key, $value) { if (is_array($value)) { $this->query->whereIn($key, $value); } return $this; }
/** * Fired just before querying * for table entries. * * @param Builder $query */ public function onQuerying(Builder $query) { $uploaded = $this->getUploaded(); $query->whereIn('id', $uploaded ?: [0]); $query->orderBy('updated_at', 'ASC'); $query->orderBy('created_at', 'ASC'); }
/** * Apply the scope to a given Eloquent query builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @return void */ public function apply(Builder $builder, Model $model) { $subclassTypes = array_keys($model->getSingleTableTypeMap()); if (!empty($subclassTypes)) { $builder->whereIn($model->getQualifiedSingleTableTypeColumn(), $subclassTypes); } }
/** * Constrain the given users query by all provided roles. * * @param \Illuminate\Database\Eloquent\Builder $query * @param string $role * @return \Illuminate\Database\Eloquent\Builder */ 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)); }
/** * 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) { if (isset($model->type_field) && isset($model->type)) { if (!is_array($model->type)) { $builder->where($model->type_field, $model->type); } else { $builder->whereIn($model->type_field, $model->type); } } elseif (isset($model->type)) { if (!is_array($model->type)) { $builder->where('type', $model->type); } else { $builder->whereIn('type', $model->type); } } }
/** * * Delete numbers of notifications equals * to the number passing as 2 parameter of * the current user * * @param $user_id int * @param $entity * @param $number int * @param $order string * @return int * @throws \Exception */ public function deleteLimit($user_id, $entity, $number, $order) { $notifications_ids = $this->notification->wherePolymorphic($user_id, $entity)->orderBy('id', $order)->select('id')->limit($number)->lists('id'); if (count($notifications_ids) == 0) { return false; } return $this->notification->whereIn('id', $notifications_ids)->delete(); }
public function build(Builder $query) { if (is_array($this->title)) { return $query->whereIn('title', $this->title); } else { return $query->where('title', '=', $this->title); } }
public function findAllBy($attribute, $value, $columns = ['*']) { $this->newQuery(); // Perform where in if (is_array($value)) { return $this->query->whereIn($attribute, $value)->get($columns); } return $this->query->where($attribute, '=', $value)->get($columns); }
/** * Apply simple filter * * @param Filter $filter */ protected function applySimpleFilter(Filter $filter) { $value = (array) $filter->getValue(); if (count($value) > 1) { $this->query->whereIn($filter->getField(), $value); } else { $this->query->where($filter->getField(), $filter->getOperator(), $value[0]); } }
public function build(Builder $query) { $query->join('assets_tags', 'assets_tags.asset_id', '=', 'assets.id'); if (is_array($this->tags)) { $query->whereIn('assets_tags.tag', $this->tags)->groupBy('tag')->having(DB::raw('count(distinct tag)'), '=', count($this->tags)); } else { $query->where('assets_tags.tag', '=', $this->tags); } return $query; }
/** * 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->whereIn($model->getStatusColumn(), [Status::APPROVED, Status::PENDING]); } $this->extend($builder); }
/** * 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); }
public function apply(Builder $builder, Model $model) { if (session()->get('site') != 1) { $builder->where('site_id', session()->get('site')); } $user = auth()->user(); if (!$user->isAdmin()) { $builder->whereIn('pub_id', $user->pubIDs()); } }
/** * Set clauses on the query builder * * @return $this */ protected function setClauses() { foreach ($this->wheres as $where) { $this->query->where($where['column'], $where['operator'], $where['value']); } foreach ($this->whereIns as $whereIn) { $this->query->whereIn($whereIn['column'], $whereIn['values']); } if (isset($this->take) and !is_null($this->take)) { $this->query->take($this->take); } return $this; }
public function scopeBySearchTerm(Builder $query, $searchTerm) { if (!is_string($searchTerm) or empty($searchTerm)) { return $query; } $itemIds = \DB::select(' SELECT distinct ' . $this->getSearchTableKeyName() . ' FROM `' . $this->getSearchTable() . '` WHERE ' . $this->generateSearchQueryPart($searchTerm)); $itemIds = collect($itemIds)->lists($this->getSearchTableKeyName()); $this->searchTermItemsCount = count($itemIds); return $query->whereIn($this->getKeyName(), $itemIds); }
/** * Applies parameter filtering for a given query * * @param string $name * @param mixed $value * @param EloquentBuilder $query * @param FilterInterface $filter * @return EloquentBuilder */ public function apply($name, $value, $query, FilterInterface $filter) { $column = (!empty($this->table) ? $this->table . '.' : null) . (!empty($this->column) ? $this->column : $name); // an array of values: do a whereIn query if (is_a($value, Arrayable::class)) { $value = $value->toArray(); } if (is_array($value)) { $query->whereIn($column, $value); return $query; } // otherwise, do a normal where return $query->where($column, $this->operator, $value); }
/** * * @param \Illuminate\Database\Eloquent\Builder $builder * @param AclPermission $permission * @param string $field * @return \Illuminate\Database\Eloquent\Builder */ public function wherePermission($builder, AclPermission $permission, $field = null) { if (!$field) { $field = $this->getAclKey(); } if ($permission->allowed && !$permission->values) { return $builder; } if ($permission->allowed) { $builder->whereIn($this->getTable() . '.' . $field, $permission->values); return $builder; } if ($permission->values) { $builder->whereNotIn($this->getTable() . '.' . $field, $permission->values); return $builder; } throw new NoPermissionsException("No perrmision to " . $permission->resource); // return $builder->whereRaw('1 = 0'); }
/** * @param Builder $query * @param array $conditions * @return mixed */ public function scopeSearch(Builder $query, array $conditions) { // 見た目 if (isset($conditions['sex_s']) and $conditions['sex_s'] !== '0') { $query->where('sex', $conditions['sex_s']); } // 年代 if (isset($conditions['age_s']) and $conditions['age_s'] !== '0') { $query->where('age', $conditions['age_s']); } // 都道府県 if (isset($conditions['prefectures_s']) and $conditions['prefectures_s'] !== '0') { $query->where('prefectures', $conditions['prefectures_s']); } // 地域 if (isset($conditions['area_s']) and $conditions['area_s'] !== '0') { $areas = config::get('const.area_prefectures'); $areaPrefectures = $areas[$conditions['area_s']]; $query->whereIn('prefectures', $areaPrefectures); } // test 投稿を無視 $query->where('name', '<>', 'test'); $hasInterests = false; $interests = Interest::all(); $keys = array_keys($conditions); foreach ($interests as $row) { if (in_array($row->name_tag, $keys)) { $hasInterests = true; break; } } if ($hasInterests) { foreach ($interests as $row) { if (in_array($row->name_tag, $keys)) { $query->whereHas('interests', function ($query) use($row) { $query->where('id', $row->id); }); } } } $query->where('res_id', 0); return $query; }
/** * Applies constraint to query. * * @param Builder $builder query builder * @param string $field field name */ public function apply(Builder $builder, $field) { if (strpos($field, ':') !== false) { list($model, $column) = explode(':', $field); $model = explode('_', $model); for ($i = 1; $i < count($model); $i++) { $model[$i] = ucfirst($model[$i]); } $model = implode($model); $field = str_replace(':', '.', $field); $builder->whereHas($model, function ($query) use($field) { $this->apply($query, $field); }); } elseif ($this->operator == Constraint::OPERATOR_IN) { $builder->whereIn($field, $this->value); } elseif ($this->operator == Constraint::OPERATOR_NOT_IN) { $builder->whereNotIn($field, $this->value); } else { $builder->where($field, $this->operator, $this->value); } }
/** * Fired just before querying * for table entries. * * @param Builder $query */ public function onQuerying(Builder $query) { $uploaded = $this->getUploaded(); if ($fieldType = $this->getFieldType()) { /** * If we have the entry available then * we can determine saved sort order. */ $entry = $fieldType->getEntry(); $table = $fieldType->getPivotTableName(); $query->join($table, $table . '.file_id', '=', 'files_files.id'); $query->where($table . '.entry_id', $entry->getId()); $query->orderBy($table . '.sort_order', 'ASC'); } else { /** * If all we have is ID then just use that. * The JS / UI will be handling the sort * order at this time. */ $query->whereIn('id', $uploaded ?: [0]); } }
/** * Set clauses on the query builder * * @return $this */ protected function setClauses() { foreach ($this->wheres as $where) { $this->query->where($where['column'], $where['operator'], $where['value']); } foreach ($this->whereIns as $whereIn) { $this->query->whereIn($whereIn['column'], $whereIn['values']); } foreach ($this->orderBys as $orders) { $this->query->orderBy($orders['column'], $orders['direction']); } if (isset($this->take) and !is_null($this->take)) { $this->query->take($this->take); } if (isset($this->skip) and !is_null($this->skip)) { $this->query->skip($this->skip); } if (isset($this->paginate) and !is_null($this->paginate)) { $this->query->paginate($this->paginate); } return $this; }
/** * Returns items for categories from database * * @param array $categoryIDs * * @return mixed */ public function getCategoriesItems(array $categoryIDs) { return $this->queryBuilder->whereIn('category_id', $categoryIDs)->get()->all(); }
public function build(Builder $query) { $pageIds = DB::table('search_texts')->select('page_id')->join('pages', 'pages.id', '=', 'search_texts.page_id')->where('internal_indexing', '=', '?')->whereRaw('match(title, standfirst, text, meta) against (?)')->where('embargoed_until', '<=', '?')->setBindings([true, $this->text, time()])->groupBy('page_id')->lists('page_id'); return $query->whereIn('pages.id', $pageIds); }
/** * Scope a query to only include activities that belongs to all centres that the specified staff are in charge of. * * @param \Illuminate\Database\Eloquent\Builder $query the query to activities to be scoped * @param \App\Staff $staff the staff to be scoped * @return \Illuminate\Database\Eloquent\Builder */ public function scopeOfCentreForStaff($query, $staff) { $query->whereIn('centre_id', $staff->centres->lists('centre_id')); }
/** * @param \Illuminate\Database\Eloquent\Builder|Builder $query * @param string $slot * @return \Illuminate\Database\Eloquent\Builder */ public function scopeInSlot(Builder $query, $slot) { return $query->whereIn('slot', (array) $slot); }
/** * @codeCoverageIgnore * * @param EloquentBuilder $query * @param array $types */ public function scopeTransactionTypes(EloquentBuilder $query, array $types) { if (is_null($this->joinedTransactionTypes)) { $query->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'); $this->joinedTransactionTypes = true; } $query->whereIn('transaction_types.type', $types); }
/** * Takes the supplied $selectedItems mixed value and formats it to a usable array * * @param \Illuminate\Database\Query\Builder $query * @param array $selectedItems * @param \Frozennode\Administrator\Fields\Field $fieldObject * @param string $relatedKeyTable * * @return array */ public function filterQueryBySelectedItems(EloquentBuilder &$query, array $selectedItems, Field $fieldObject, $relatedKeyTable) { $query->whereIn($relatedKeyTable, $selectedItems); //if this is a BelongsToMany and a sort field is set, order it by the sort field if ($fieldObject->getOption('multiple_values') && $fieldObject->getOption('sort_field')) { $query->orderBy($fieldObject->getOption('sort_field')); } else { $query->orderBy($fieldObject->getOption('name_field')); } }