Skip to content

Commit b1deaa2

Browse files
committed
Enhance latest() and oldest() methods to accept a single integer argument as limit
1 parent 45d4b78 commit b1deaa2

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/Illuminate/Database/Query/Builder.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2724,6 +2724,13 @@ public function orderByDesc($column)
27242724
*/
27252725
public function latest($column = 'created_at', $limit = null)
27262726
{
2727+
// If only one integer argument is passed, treat it as the limit and default the column to 'created_at'.
2728+
// This allows calling "latest(11)" instead of "latest('created_at', 11)" or "latest(limit: 11)"
2729+
if (is_null($limit) && is_int($column)) {
2730+
$limit = $column;
2731+
$column = 'created_at';
2732+
}
2733+
27272734
if (! is_null($limit)) {
27282735
$this->limit($limit);
27292736
}
@@ -2740,6 +2747,13 @@ public function latest($column = 'created_at', $limit = null)
27402747
*/
27412748
public function oldest($column = 'created_at', $limit = null)
27422749
{
2750+
// If only one integer argument is passed, treat it as the limit and default the column to 'created_at'.
2751+
// This allows calling "oldest(11)" instead of "oldest('created_at', 11)" or "oldest(limit: 11)"
2752+
if (is_null($limit) && is_int($column)) {
2753+
$limit = $column;
2754+
$column = 'created_at';
2755+
}
2756+
27432757
if (! is_null($limit)) {
27442758
$this->limit($limit);
27452759
}

0 commit comments

Comments
 (0)