/** * Get records from the database. * * @param int $per_page * * @return Collection */ public function get($per_page = 20) { $this->per_page = $per_page; $this->buildQuery(); return $this->query->get(); }
/** * @param Builder|EloquentBuilder $data * @return Traversable */ protected function afterOperations($data) { if ($data instanceof EloquentBuilder) { return $data->get(); } elseif ($data instanceof Builder) { return new ArrayIterator($data->get()); } throw new RuntimeException('Unsupported type of data source.'); }
public function totalCount() { if ($this->options['distinctCountGroup'] && count($this->originalBuilder->groups) == 1) { $this->originalBuilder->groups = null; } if ($this->options['searchWithAlias']) { $cnt = count($this->originalBuilder->get()); } else { $cnt = $this->originalBuilder->count(); } return $cnt; }
/** * Find data by multiple fields * * @param array $where * @param array $columns * @return mixed|Model */ public function findWhere(array $where, $columns = ['*']) { return $this->wrap(function ($where, $columns = ['*']) { $this->applyWhere($where); return $this->model->get($columns); }, new Action(__METHOD__, func_get_args(), Action::READ)); }
/** * Gets results from prepared query * * @return null */ protected function getResult() { if ($this->query_type == 'eloquent') { $this->result_object = $this->query->get(); $this->result_array = $this->result_object->toArray(); } else { $this->result_object = $this->query->get(); $this->result_array = array_map(function ($object) { return (array) $object; }, $this->result_object); } if ($this->dataFullSupport) { $walk = function ($value, $key, $prefix = null) use(&$walk, &$result_array) { $key = !is_null($prefix) ? $prefix . "." . $key : $key; if (is_array($value)) { array_walk($value, $walk, $key); } else { $result_array = Arr::add($result_array, $key, $value); } }; $result_array = array(); array_walk($this->result_array, $walk); $this->result_array = $result_array; } }
/** * Get all the specified model records in the database * * @return \Illuminate\Database\Eloquent\Collection */ public function get() { $this->newQuery()->eagerLoad()->setClauses()->setScopes(); $models = $this->query->get(); $this->unsetClauses(); return $models; }
/** * @return \ArrayIterator|mixed|null */ public function getEntities() { $result = $this->specificationQuery->get(); if (0 === $result->count()) { return null; } return $this->mapper->fromDbTableRows($result); }
/** * Return the elements * * @return Collection */ public function get() { // Save the results from the builder $results = $this->builder->get(); // Reset the builder, just in case we are going to use this repository more then once $this->newBuilder(); // Return stuff return $results; }
/** * Return a model (or collection of models) for given query or throw an exception. * * @param \Illuminate\Database\Eloquent\Builder $query * @param int $expectedNoElements * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection * * @throws \Illuminate\Database\Eloquent\ModelNotFoundException */ public function findByQueryOrFail($query, $expectedNoElements = 1) { $result = $query->get(); $noElements = count($result); if ($noElements and ($noElements === $expectedNoElements or !is_int($expectedNoElements))) { return $result; } throw (new ModelNotFoundException())->setModel(get_class($query->getModel())); }
public function items(Builder $query) { $items = []; $ids = $query->get(['users.id']); foreach ($ids->pluck('id')->all() as $id) { $items[] = $this->item($id); } return $items; }
/** * Execute the query as a "select" statement. * * @param array $columns * @return \Illuminate\Database\Eloquent\Collection|static[] */ public function get($columns = array('*')) { $results = parent::get($columns); switch ($this->returnType) { case Builder::RETURN_TYPE_DATAMAPPER: return $results->toDatamapperObject(); case Builder::RETURN_TYPE_ELOQUENT: return $results; } }
/** * * @param array $columns * @return \Illuminate\Database\Eloquent\Collection|static[] */ public function get($columns = ['*']) { $this->addCountSub(); return parent::get($columns)->map(function ($element) { foreach ($this->counts as $relation_name => $saved_as) { $element->saveCountAssociatedObject($relation_name, $element[$saved_as]); unset($element[$saved_as]); } return $element; }); }
/** * Paginate the results of this query * * @param int * @param int * @return $this */ public function paginate($perPage, $page) { if ($perPage > 0) { Paginator::currentPageResolver(function () use($page) { return $page; }); $this->paginator = $this->query->paginate($perPage); } else { $this->collection = $this->query->get(); } return $this; }
/** * Execute the query as a "select" statement. * * @param array $columns * @return \Illuminate\Database\Eloquent\Collection|static[] */ public function get($columns = ['*']) { $this->orderByDefault(); if (env('DB_CACHE')) { $this->rememberIndex(); if ($this->model->getTtl()) { try { return app('cache')->remember($this->getCacheKey(), $this->model->getTtl(), function () use($columns) { return parent::get($columns); }); } catch (\Exception $e) { return parent::get($columns); } } } return parent::get($columns); }
/** * Create a new Eloficient builder instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @return void */ public function get($columns = array("*")) { if ($this->disableEloficient) { return parent::get($columns); } if ($this->eagerLoad) { $this->prepareQuery(); $this->buildRelationshipTree(); $this->applyRelationshipQuery($this->relations); $this->reformatQueryComponents(); $this->query->columns = array_merge($this->getColumns($this->relations), $this->getObserverColumns()); $this->applySearch(); $models = $this->buildModelsFromRelationshipTree($this->relations, $results = $this->query->get()); } else { $models = $this->getModels($columns); } return $this->model->newCollection($models); }
/** * @param Closure $closure * @param null $cacheKey * @return Collection|null|static[] * * * allows for custom repository querying with optional caching */ public function customGet(Closure $closure, $cacheKey = null) { $this->cacheKey = $cacheKey; if ($this->cacheKey && $this->cache) { $this->cacheKey = "~[{$this->cacheKey}]"; if ($this->paginate) { $this->cacheKey .= 'p=' . (Paginator::resolveCurrentPage() ?: 1) . "&pp=" . $this->perPage; } if ($cache = Cache::get($this->cacheKey)) { return $cache === 'null' ? null : $cache; } } $this->model = (new $this->modelClass())->newQuery(); $closure($this->model); $result = $this->model->get(); $this->storeToCache($result); return $result; }
/** * Возвращает коллекцию в виде пагинации * * @param int $page * @param int $limit */ public function pagination($page, $limit = 10) { /** * @var \Illuminate\Support\Collection $data */ $countTotal = $this->_query->count(); $this->_query->skip($limit * $page - $limit); $this->_query->limit($limit); $data = collect(); foreach ($this->_query->get() as $key => $instance) { $_listRow = []; foreach ($this->columns->getColumns() as $column) { $_listRow[$column->getKey()] = $column->getValues($instance); } $buttons = $this->filterAction($instance); if (count($buttons)) { $_listRow = array_merge($_listRow, [GridColumn::ACTION_NAME => implode('', $buttons)]); } $data->offsetSet($key, $_listRow); } return new \Illuminate\Pagination\LengthAwarePaginator($data, $countTotal, $limit, $page, ['path' => \Illuminate\Pagination\Paginator::resolveCurrentPath(), 'pageName' => 'page']); }
public function apply(Query $query, BaseRepository $repository) { $meta = false; if (isset($this->filter['meta'])) { $meta = $this->filter['meta']; unset($this->filter['meta']); } if (!empty($this->filter)) { $query->where($this->filter); } if (!empty($meta)) { $table = $query->getModel()->getTable(); $query->join('meta', $table . '.id', '=', 'meta.object_id'); $metaQuery = $query->getQuery()->newQuery(); foreach ($meta as $k => $v) { $metaQuery->orWhere(['meta.key' => $k, 'meta.value' => $v]); } $query->addNestedWhereQuery($metaQuery); $query->groupBy($table . '.id'); $results = $query->get(); } }
public function all($columns = ['*']) { $this->newQuery(); return $this->query->get($columns); }
/** * Execute the query as a "select" statement. * * @param array $columns * @return \Illuminate\Database\Eloquent\Collection|static[] */ public function get($columns = array('*')) { $collection = parent::get($columns); return $this->getNormalizer()->normalize($collection->toArray()); }
/** * Query execution function called by getters. * All repository getters should call this for integrated caching at the repository level. * * @param \Illuminate\Database\Eloquent\Builder $query * @param array $columns The columns to retrieve * @param bool $first Limit to one result or not * @return \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|null */ protected function executeQuery($query, $first) { $tag = $this->cacheTag ?: $this->untagged; $builder = $query->getQuery(); $key = $this->getQueryKey($builder, $first); if ($first) { return Cache::tags(['repositories', $tag])->remember($key, $this->cacheDuration, function () use($query) { return $query->first(); }); } else { return Cache::tags(['repositories', $tag])->remember($key, $this->cacheDuration, function () use($query) { return $query->get(); }); } }
/** * @param $builder * @param array $options * * @return array */ protected function paginate(Builder $builder, array $options) { $query = $builder->getQuery(); $total = $query->getCountForPagination(); $perPage = array_get($options, 'per_page', $this->getPerPage()); $lastPage = (int) ceil($total / $perPage); $page = max(1, min($lastPage, (int) array_get($options, 'page', 1))); $query->forPage($page, $perPage); /** @var \Illuminate\Support\Collection $items */ $items = $builder->get(); $from = ($page - 1) * $perPage + 1; $to = $from + $items->count() - 1; return compact('total', 'page', 'perPage', 'lastPage', 'from', 'to', 'items'); }
/** * Gets the query result. * * @param $columns * @return mixed */ public function get($columns = array('*')) { $collection = $this->query->get($columns); $this->resetScope(); return $collection; }
/** * Execute the query as a "select" statement. * * @param array $columns * @return \Illuminate\Database\Eloquent\Collection */ public function get($columns = ['*']) { if ($this->query->from instanceof Subquery) { $this->wheresToSubquery($this->query->from); } return parent::get($columns); }
/** * @param array $columns * @return mixed */ public function all($columns = array('*')) { $this->applyCriteria(); $this->newQuery()->eagerLoadRelations(); return $this->model->get($columns); }
/** * @param array $columns * @return mixed */ public function all($columns = array('*')) { $this->applyCriteria(); return $this->execute($this->model->get($columns)); }
/** * Either purge all the records at once or loop through them one by one. * * This is to allow events to get fired for each record if needed. * * @param Builder $query * @param string $model_name * * @return int */ protected function purgeRecordsAsConfigured(Builder $query, $model_name) { if ($this->fire_purge_events !== true) { $this->recordMessage("Deleting all the records in a single query statement."); return $query->forceDelete(); } $this->recordMessage("Deleting each record separately and firing events."); $records = $query->get(); foreach ($records as $record) { $this->firePurgeEvent('purging', $model_name, $record); $record->forceDelete(); $this->firePurgeEvent('purged', $model_name, $record); } return $records->count(); }
/** * @param array $columns * @return Model|Collection */ public function get($columns = ['*']) { return parent::get($columns); }
/** * Execute the query as a "select" statement. * * @param array $columns * @return \Illuminate\Database\Eloquent\Collection|static[] * @static */ public static function get($columns = array()) { return \Illuminate\Database\Eloquent\Builder::get($columns); }
/** * @param Builder $builder * @return EloquentCollection */ protected function all(Builder $builder) { return $builder->get(); }