[laravel] Select Last Row in the Table

I would like to retrieve the last file inserted into my table. I know that the method first() exists and provides you with the first file in the table but I don't know how to get the last insert.

This question is related to laravel eloquent

The answer is


You can use the latest scope provided by Laravel with the field you would like to filter, let's say it'll be ordered by ID, then:

Model::latest('id')->first();

So in this way, you can avoid ordering by created_at field by default at Laravel.


You never mentioned whether you are using Eloquent, Laravel's default ORM or not. In case you are, let's say you want to get the latest entry of a User table, by created_at, you probably could do as follow:

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

First it orders users by created_at field, descendingly, and then it takes the first record of the result.

That will return you an instance of the User object, not a collection. Of course, to make use of this alternative, you got to have an User model, extending Eloquent class. This may sound a bit confusing, but it's really easy to get started and ORM can be really helpful.

For more information, check out the official documentation which is pretty rich and well detailed.


Use Model::where('user_id', $user_id)->latest()->get()->first(); it will return only one record, if not find, it will return null. Hope this will help.


Another fancy way to do it in Laravel 6.x (Unsure but must work for 5.x aswell) :

DB::table('your_table')->get()->last();

You can access fields too :

DB::table('your_table')->get()->last()->id;


For laravel 8:

Model::orderBy('id', 'desc')->withTrashed()->take(1)->first()->id

The resulting sql query:

Model::orderBy('id', 'desc')->withTrashed()->take(1)->toSql()

select * from "timetables" order by "id" desc limit 1


To get the last record details, use the code below:

Model::where('field', 'value')->get()->last()

If the table has date field, this(User::orderBy('created_at', 'desc')->first();) is the best solution, I think. But there is no date field, Model ::orderBy('id', 'desc')->first()->id; is the best solution, I am sure.


To get last record details

  1. Model::all()->last(); or
  2. Model::orderBy('id', 'desc')->first();

To get last record id

  1. Model::all()->last()->id; or
  2. Model::orderBy('id', 'desc')->first()->id;

Use the latest scope provided by Laravel out of the box.

Model::latest()->first();

That way you're not retrieving all the records. A nicer shortcut to orderBy.


Don't use Model::latest()->first(); because if your collection has multiple rows created at the same timestamp (this will happen when you use database transaction DB::beginTransaction(); and DB::commit()) then the first row of the collection will be returned and obviously this will not be the last row.

Suppose row with id 11, 12, 13 are created using transaction then all of them will have the same timestamp so what you will get by Model::latest()->first(); is the row with id: 11.


Laravel collections has method last

Model::all() -> last(); // last element 
Model::all() -> last() -> pluck('name'); // extract value from name field. 

This is the best way to do it.


Model($where)->get()->last()->id


Try this :

Model::latest()->get();

If you are looking for the actual row that you just inserted with Laravel 3 and 4 when you perform a save or create action on a new model like:

$user->save();

-or-

$user = User::create(array('email' => '[email protected]'));

then the inserted model instance will be returned and can be used for further action such as redirecting to the profile page of the user just created.

Looking for the last inserted record works on low volume system will work almost all of the time but if you ever have to inserts go in at the same time you can end up querying to find the wrong record. This can really become a problem in a transactional system where multiple tables need updated.


be aware that last(), latest() are not deterministic if you are looking for a sequential or event/ordered record. The last/recent records can have the exact same created_at timestamp, and which you get back is not deterministic. So do orderBy(id|foo)->first(). Other ideas/suggestions on how to be deterministic are welcome.


Somehow all the above doesn't seem to work for me in laravel 5.3, so i solved my own problem using:

Model::where('user_id', '=', $user_id)->orderBy('created_at', 'desc')->get();

hope am able to bail someone out.