Create a new Eloquent Collection instance.
public newCollection ( array $models = [] ) : Illuminate\Database\Eloquent\Collection | ||
$models | array | |
return | Illuminate\Database\Eloquent\Collection |
/** * Use the custom collection that allows tapping. * * @param array $models An array of models to turn into a collection. * * @return \Illuminate\Database\Eloquent\Collection */ public function newCollection(array $models = []) { if ($this->nukaCollections) { return new Collection($models); } return parent::newCollection($models); }
/** * Find multiple models by their primary keys. * * @param array $ids * @param array $columns * @return \Illuminate\Database\Eloquent\Collection */ public function findMany($ids, $columns = ['*']) { if (empty($ids)) { return $this->model->newCollection(); } $this->query->whereIn($this->model->getQualifiedKeyName(), $ids); return $this->get($columns); }
/** * Get descendants of specified node. * * @since 2.0 * * @param mixed $id * @param array $columns * * @return \Kalnoy\Nestedset\Collection */ public function descendantsOf($id, array $columns = array('*')) { try { return $this->whereDescendantOf($id)->get($columns); } catch (ModelNotFoundException $e) { return $this->model->newCollection(); } }
/** * Paginate the given query. * * @param int $perPage * @param array $columns * @param string $pageName * @param int|null $page * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator * * @throws \InvalidArgumentException */ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) { $page = $page ?: Paginator::resolveCurrentPage($pageName); $perPage = $perPage ?: $this->model->getPerPage(); $query = $this->toBase(); $total = $query->getCountForPagination(); $results = $total ? $this->forPage($page, $perPage)->get($columns) : $this->model->newCollection(); return new LengthAwarePaginator($results, $total, $perPage, $page, ['path' => Paginator::resolveCurrentPath(), 'pageName' => $pageName]); }
/** * Execute the query as a "select" statement. * * @param array $columns * @return \Illuminate\Database\Eloquent\Collection|static[] */ public function get($columns = array('*')) { $models = $this->getModels($columns); // If we actually found models we will also eager load any relationships that // have been specified as needing to be eager loaded, which will solve the // n+1 query issue for the developers to avoid running a lot of queries. if (count($models) > 0) { $models = $this->eagerLoadRelations($models); } return $this->model->newCollection($models); }
/** * Delete a model by the following: * $model itself when a model is given * [$model[,...]] array or collection of models * $id id of the model * [id1[, id2, ...]] array of ids. * * @param mixed $model * @return bool|int Boolean when model is deleted or the number of models deleted */ public function delete($model) { // First use-case, itself when a model is given if ($model instanceof $this->model) { return $model->delete(); } // Second use-case, array or collection of models $models = $model; if (is_array($models) && head($models) instanceof $this->model) { $models = $this->model->newCollection($models); } if ($models instanceof Collection) { $model = $models->pluck('id'); // Pass ids to $model to be deleted below on third and fourth use-case } // Third and fourth use-case, id or array of ids $ids = $model; if (is_int($model)) { $ids = [$ids]; } if (!empty($ids)) { return $this->model->destroy($ids); } }