Skip to content

Commit

Permalink
Implements take as alias to Eloquent limit (#754)
Browse files Browse the repository at this point in the history
added take method
  • Loading branch information
stephandesouza authored Apr 22, 2021
1 parent ad55bf2 commit 2bd5680
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions src/Prettus/Repository/Eloquent/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -863,13 +862,36 @@ 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);

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
*
Expand Down

0 comments on commit 2bd5680

Please sign in to comment.