ChatGPT解决这个技术问题 Extra ChatGPT

Laravel: How to get last N entries from DB

I have table of dogs in my DB and I want to retrieve N latest added dogs.

Only way that I found is something like this:

Dogs:all()->where(time, <=, another_time);

Is there another way how to do it? For example something like this Dogs:latest(5);

Thank you very much for any help :)

A combination of the orderBy() and limit() methods; though you do also have latest() as an alternative to orderBy()

T
The Alpha

You may try something like this:

$dogs = Dogs::orderBy('id', 'desc')->take(5)->get();

Use orderBy with Descending order and take the first n numbers of records.

Update (Since the latest method has been added):

$dogs = Dogs::latest()->take(5)->get();

What happens if there are only 3 records in this case?
You'll get three :-)
This will reverse order of your items. You will have to re-order them in your application. So if you're trying to load last 5 posts sorted by date, don't forget to re-sort them on collection level.
p
parker_codes

My solution for cleanliness is:

Dogs::latest()->take(5)->get();

It's the same as other answers, just with using built-in methods to handle common practices.


This also works with UUIDs so it's more generic. It doesn't work with tables that don't have timestamps though.
L
Luca C.
Dogs::orderBy('created_at','desc')->take(5)->get();

J
João Marcus

You can pass a negative integer n to take the last n elements.

Dogs::all()->take(-5)

This is good because you don't use orderBy which is bad when you have a big table.


it is not warranted that the last 5 resulted are the last 5 inserted. it USUALLY works, but with no warranty
I had no success using it. To me -5 and 5 works the same !!!
B
Bablu Ahmed

You may also try like this:

$recentPost = Article::orderBy('id', 'desc')->limit(5)->get();

It's working fine for me in Laravel 5.6


Yes, method take and limit is similar. The parent is limit, and take is just alias. You can check on file Builder.php
h
hackernewbie

I use it this way, as I find it cleaner:

$covidUpdate = COVIDUpdate::latest()->take(25)->get();

k
kray

Ive come up with a solution that helps me achieve the same result using the array_slice() method. In my code I did array_slice( PickupResults::where('playerID', $this->getPlayerID())->get()->toArray(), -5 ); with -5 I wanted the last 5 results of the query.


M
Michael Engelbrecht

The Alpha's solution is very elegant, however sometimes you need to re-sort (ascending order) the results in the database using SQL (to avoid in-memory sorting at the collection level), and an SQL subquery is a good way to achieve this.

It would be nice if Laravel was smart enough to recognise we want to create a subquery if we use the following ideal code...

$dogs = Dogs::orderByDesc('id')->take(5)->orderBy('id')->get();

...but this gets compiled to a single SQL query with conflicting ORDER BY clauses instead of the subquery that is required in this situation.

Creating a subquery in Laravel is unfortunately not simply as easy as the following pseudo-code that would be really nice to use...

$dogs = DB::subQuery( 
    Dogs::orderByDesc('id')->take(5) 
)->orderBy('id');

...but the same result can be achieved using the following code:

$dogs = DB::table('id')->select('*')->fromSub(
    Dogs::orderByDesc('id')->take(5)->toBase(), 
    'sq'
)->orderBy('id');

This generates the required SELECT * FROM (...) AS sq ... sql subquery construct, and the code is reasonably clean in terms of readability.)

Take particular note of the use of the ->toBase() function - which is required because fromSub() doesn't like to work with Eloquent model Eloquent\Builder instances, but seems to require a Query\Builder instance). (See: https://github.com/laravel/framework/issues/35631)

I hope this helps someone else, since I just spent a couple of hours researching how to achieve this myself. (I had a complex SQL query builder expression that needed to be limited to the last few rows in certain situations).


D
Dharman

Imagine a situation where you want to get the latest record of data from the request header that was just inserted into the database:

$noOfFilesUploaded = count( $request->pic );// e.g 4
$model = new Model;

$model->latest()->take($noOfFilesUploaded); 

This way your take() helper function gets the number of array data that was just sent via the request.

You can get only ids like so:

$model->latest()->take($noOfFilesUploaded)->puck('id')


M
Mosaib Khan

For getting last entry from DB

$variable= Model::orderBy('id', 'DESC')->limit(1)->get();


R
RJH
use DB;

$dogs = DB::select(DB::raw("SELECT * FROM (SELECT * FROM dogs ORDER BY id DESC LIMIT 10) Var1 ORDER BY id ASC"));

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.