Skip to content

Conversation

@theHocineSaad
Copy link
Contributor

This PR introduces a new $limit parameter to the latest() and oldest() methods, allowing developers to limit results without chaining take().


Before

Previously, limiting results required chaining take() after latest() or oldest():

// Using Eloquent
User::latest()->take(10)->get();

// Using Query Builder
DB::table('users')->latest()->take(10)->get();

// Oldest examples
User::oldest()->take(10)->get();
DB::table('users')->oldest()->take(10)->get();

After

Now, you can pass the limit directly as an argument to latest() or oldest().

// Specify both column and limit
User::latest('updated_at', 10)->get();
DB::table('users')->latest('updated_at', 10)->get();

User::oldest('updated_at', 10)->get();
DB::table('users')->oldest('updated_at', 10)->get();

You can also use named arguments for clarity:

User::latest(limit: 10)->get();
DB::table('users')->latest(limit: 10)->get();

User::oldest(limit: 10)->get();
DB::table('users')->oldest(limit: 10)->get();

Shortcut

If you don’t need to change the default column (created_at), you can pass only the limit:

// Defaults to ordering by 'created_at'
User::latest(10)->get();
DB::table('users')->latest(10)->get();

User::oldest(10)->get();
DB::table('users')->oldest(10)->get();

@browner12
Copy link
Contributor

We have amazing composability with chaining, this is not needed.

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

@theHocineSaad theHocineSaad deleted the add-limit-to-latest-and-oldest branch September 12, 2025 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants