Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select second last record with Laravel - eloquent

How can i select the next to last record added to a table. I can select the last record like -

transition::order_by('created_at', 'desc')->first();
like image 815
Prakhar Thakur Avatar asked Nov 23 '16 07:11

Prakhar Thakur


People also ask

How do I change the last row in Laravel?

$lastUser = App\User::orderBy('created_at', 'desc')->first(); after you can update your model like this: $lastUser->name = "New Name"; $lastUser->save(); UPDATE: you can also use the id field instead of 'created_at' for ordering your data.

What is latest () in Laravel?

->latest() fetches the most recent set of data from the Database. In short, it sorts the data fetched, using the 'created_at' column to chronologically order the data.

How do I skip the first record in Laravel?

skip() will help to skip some records when you fetch data from the database table. So, let's see bellow examples that will help you how to use take() and skip() eloquent query in laravel.


1 Answers

Try this:

transition::orderBy('created_at', 'desc')->skip(1)->take(1)->get();
like image 119
Rimon Khan Avatar answered Sep 24 '22 00:09

Rimon Khan