private function filter(\Illuminate\Database\Eloquent\Builder $_db) { $_db->take($this->limit); if (Request::get('offset')) { $_db->skip(Request::get('offset')); } return $_db; }
/** * 对query进行分页操作但不最终运行 * * @param Builder $query query * @param int $page page * @param int $take take * * @return Builder */ public static function paging($query, $page, $take) { if ($page < 1) { $page = 1; } $skip = ($page - 1) * $take; return $query->take($take)->skip($skip); }
/** * Load connection w/ args. * * @param \Illuminate\Database\Eloquent\Builder $query * @param array $args * @return \Illuminate\Database\Eloquent\Builder */ public function scopeLoadConnection($query, array $args) { $first = isset($args['first']) ? $args['first'] : 15; $after = $this->decodeCursor($args); $page = isset($args['page']) ? $args['page'] : 1; $currentPage = $first && $after ? floor(($first + $after) / $first) : $page; $skip = $first * $currentPage; return $query->take($first)->skip($skip); }
/** * @param $method * @param $parameters * @return Collection * @throws \Exception * * sets cache key if derived class method call * derived classes methods (like 'featured') are to be used as 'getFeatured' */ public function __call($method, $parameters) { $realMethod = lcfirst(substr($method, 3)); if (!method_exists($this, $realMethod)) { throw new \Exception("Method does not exist - " . $method); } if ($this->cache) { $this->cacheKey = ($this->onlyVisible ? '-' : '+') . ($this->onlyAuthored ? '-' : '+') . $this->baseName . ".{$realMethod}[" . serialize($parameters) . "]" . "\\{" . serialize($this->filters) . "\\}" . "<" . implode(',', $this->excludes) . ">"; if ($this->limit) { $this->cacheKey .= "|{$this->limit}|"; } if ($this->paginate) { $this->cacheKey .= 'p=' . (Paginator::resolveCurrentPage() ?: 1) . "&pp=" . $this->perPage; } if ($this->closure) { $this->cacheKey .= "{{$this->keyFragment}}"; } if ($cache = Cache::get($this->cacheKey)) { $this->clearFilters(); if ($this->appendExcludes) { $this->excludes = array_merge($this->excludes, $cache->lists($this->identifier)->all()); $this->appendExcludes = false; } return $cache === 'null' ? null : $cache; } } $this->model = new $this->modelClass(); $table = $this->model->getTable(); $this->model = $this->model->newQuery(); $this->model->select("{$table}.*"); $this->model = $this->model->whereNotIn($table . '.' . $this->identifier, $this->excludes); $this->prepare($this->model); $this->filter($this->model, $this->filters); if ($this->onlyVisible) { $this->filterVisible($this->model); } if ($this->onlyAuthored) { $this->filterAuthored($this->model); } if ($this->limit) { $this->model->take($this->limit); } array_unshift($parameters, $this->model); call_user_func_array(array($this, $realMethod), $parameters); if ($this->closure) { $closure = $this->closure; $closure($this->model); } $result = $this->get(); if ($this->appendExcludes) { $this->excludes = array_merge($this->excludes, $result->lists($this->identifier)->all()); $this->appendExcludes = false; } $this->clearFilters(); return $result; }
private function filter(\Illuminate\Database\Eloquent\Builder $_db) { $_db->take($this->limit); if (Request::get('offset')) { $_db->skip(Request::get('offset')); } if (Request::get('q')) { $_db->where('description', 'like', Request::get('q') . '%'); } return $_db; }
public function build() { $request = $this->request; $needle = $request->needle(); if ($needle && $request->searchable) { $this->qb->where(function ($subQB) use($needle, $request) { foreach ($request->searchable as $column) { $subQB->orWhere($column, 'LIKE', "%{$needle}%"); } }); } $request->modifyQuery($this->qb); $this->qb->orderBy($request->orderBy(), $request->sortBy()); $count = $this->qb->count(); $this->qb->take($request->perPage()); $this->qb->offset($request->offset()); $paginator = new LengthAwarePaginator($request->items($this->qb), $count, $request->perPage(), $request->page()); $paginator->request = $request; $paginator->appends($request->query()); $this->paginator = $paginator; }
/** * 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; }
private function buildQuery() { $this->query = app(get_class($this->model)); if (!empty($this->fields)) { $this->query = $this->query->select($this->fields); } if (!empty($this->relations)) { $this->relations = array_unique($this->relations); $this->query = $this->query->with($this->relations); } if (!empty($this->per_page)) { $this->query = $this->query->take($this->per_page); } if (count($this->conditions)) { foreach ($this->conditions as $condition) { $this->query = $this->query->where($condition['column'], $condition['operator'], $condition['value'], $condition['boolean']); } } }
/** * 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; }
/** * Filters a relationship options query by a search term * * @param mixed $term * @param \Illuminate\Database\Query\Builder $query * @param \Frozennode\Administrator\Fields\Field $fieldObject * @param array $selectedItems * @param string $relatedKeyTable */ public function filterBySearchTerm($term, EloquentBuilder &$query, Field $fieldObject, array $selectedItems, $relatedKeyTable) { if ($term) { $query->where(function ($query) use($term, $fieldObject) { foreach ($fieldObject->getOption('search_fields') as $search) { $query->orWhere($this->db->raw($search), 'LIKE', '%' . $term . '%'); } }); //exclude the currently-selected items if there are any if (count($selectedItems)) { $query->whereNotIn($relatedKeyTable, $selectedItems); } //set up the limits $query->take($fieldObject->getOption('num_options') + count($selectedItems)); } }
/** * Limit the number of results * * @param \Illuminate\Database\Eloquent\Builder $query * @param int $limit * @return \Illuminate\Database\Eloquent\Builder */ public function scopeLimit(Builder $query, $limit) { return $query->take($limit); }
/** * Query limit and offset. * * @param \Illuminate\Database\Eloquent\Builder $query * @param int|null $limit * @param int $offset * @return \Illuminate\Database\Eloquent\Builder */ protected function queryLimitOffset($query, $limit = null, $offset = 0) { $limit = $limit ?: $this->limit; $offset = $offset ?: $this->offset; if ($limit) { $query->take($limit)->skip($offset); } return $query; }
private function filter(Builder $builder, $filters) { if (isset($filters['take'])) { $builder->take($filters['take']); } if (isset($filters['category'])) { $builder->where('category_id', Category::where('slug', $filters['category'])->firstOrfail()->id); } if (isset($filters['online']) && $filters['online'] == true) { $builder->online(); } if (isset($filters['order'])) { $builder->orderBy($filters['order'], 'desc'); } else { $builder->latest('published_at'); } return $builder; }
/** * Will get entries from a specified page * * @param integer $page * @param integer $perPage * @return $this */ public function page($page, $perPage = 15) { $offset = $page * $perPage - $perPage; $this->builder = $this->builder->take($perPage)->skip($offset); return $this; }