From 2bd56801c78becf6a26b0d795312d879e55730cd Mon Sep 17 00:00:00 2001 From: Stephan de Souza Date: Thu, 22 Apr 2021 18:15:10 -0300 Subject: [PATCH] Implements `take` as alias to Eloquent `limit` (#754) added take method --- .../Repository/Eloquent/BaseRepository.php | 40 ++++++++++++++----- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/src/Prettus/Repository/Eloquent/BaseRepository.php b/src/Prettus/Repository/Eloquent/BaseRepository.php index abfe246b..7bb84896 100644 --- a/src/Prettus/Repository/Eloquent/BaseRepository.php +++ b/src/Prettus/Repository/Eloquent/BaseRepository.php @@ -450,20 +450,19 @@ public function firstOrCreate(array $attributes = []) } /** - * Set the "limit" value of the query. + * Retrieve data of repository with limit applied + * + * @param int $limit + * @param array $columns * - * @param int $value * @return mixed */ - public function limit($limit) + public function limit($limit, $columns = ['*']) { - $this->applyCriteria(); - $this->applyScope(); - $results = $this->model->limit($limit); - - $this->resetModel(); + // Shortcut to all with `limit` applied on query via `take` + $this->take($limit); - return $this->parserResult($results); + return $this->all($columns); } /** @@ -863,6 +862,14 @@ public function hidden(array $fields) return $this; } + /** + * Set the "orderBy" value of the query. + * + * @param mixed $column + * @param string $direction + * + * @return $this + */ public function orderBy($column, $direction = 'asc') { $this->model = $this->model->orderBy($column, $direction); @@ -870,6 +877,21 @@ public function orderBy($column, $direction = 'asc') return $this; } + /** + * Set the "limit" value of the query. + * + * @param int $limit + * + * @return $this + */ + public function take($limit) + { + // Internally `take` is an alias to `limit` + $this->model = $this->model->limit($limit); + + return $this; + } + /** * Set visible fields *