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 :)
You may try something like this:
Use
orderBy
withDescending
order and take the firstn
numbers of records.My solution for cleanliness is:
It's the same as other answers, just with using built-in methods to handle common practices.
You may also try like this:
It's working fine for me in Laravel 5.6
You can pass a negative integer n to take the last n elements.
This is good because you don't use orderBy which is bad when you have a big table.
Ive come up with a solution that helps me achieve the same result using the
array_slice()
method. In my code I didarray_slice( PickupResults::where('playerID', $this->getPlayerID())->get()->toArray(), -5 );
with-5
I wanted the last 5 results of the query.