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

Viewed 356

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?

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.

you can simply skip the first one from the results:

Table::orderBy('created_at', 'desc')->skip(1)->get();
Related