Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all but the most recent row in a database using Eloquent in Laravel

I want to select all rows from a mysql table except for the most recent one by using Eloquent in Laravel. I currently do this to get the most recent entry only, but wish to do the reverse:

Table::orderBy('created_at', 'desc')->first(); 

Any idea how I do this?

like image 560
Thomas Read Avatar asked Aug 31 '25 02:08

Thomas Read


2 Answers

You can make use of laravel collections shift method.

$data = DB::table('your_table')->orderBy('created_at', 'desc')->get();
$data->shift(); //which removes the first element. now $data has all other values except for first one.
like image 151
mosharaf13 Avatar answered Sep 02 '25 17:09

mosharaf13


you can simply skip the first one from the results:

Table::orderBy('created_at', 'desc')->skip(1)->get();
like image 30
OMR Avatar answered Sep 02 '25 15:09

OMR