Skip to content

[9.x] Add $limit to latest() and oldest() #39006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,19 @@ public function orWhere($column, $operator = null, $value = null)
* Add an "order by" clause for a timestamp to the query.
*
* @param string|\Illuminate\Database\Query\Expression $column
* @param int $limit
* @return $this
*/
public function latest($column = null)
public function latest($column = null, $limit = null)
{
if (is_null($column)) {
$column = $this->model->getCreatedAtColumn() ?? 'created_at';
}

if (! is_null($limit)) {
$this->query->limit($limit);
}

$this->query->latest($column);

return $this;
Expand All @@ -299,14 +304,19 @@ public function latest($column = null)
* Add an "order by" clause for a timestamp to the query.
*
* @param string|\Illuminate\Database\Query\Expression $column
* @param int $limit
* @return $this
*/
public function oldest($column = null)
public function oldest($column = null, $limit = null)
{
if (is_null($column)) {
$column = $this->model->getCreatedAtColumn() ?? 'created_at';
}

if (! is_null($limit)) {
$this->query->limit($limit);
}

$this->query->oldest($column);

return $this;
Expand Down