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.
18 Answers
You'll need to order by the same field you're ordering by now, but descending.
As an example, if you have a time stamp when the upload was done called upload_time
, you'd do something like this;
For Pre-Laravel 4
return DB::table('files')->order_by('upload_time', 'desc')->first();
For Laravel 4 and onwards
return DB::table('files')->orderBy('upload_time', 'desc')->first();
For Laravel 5.7 and onwards
return DB::table('files')->latest('upload_time')->first();
This will order the rows in the files table by upload time, descending order, and take the first one. This will be the latest uploaded file.
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.
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.
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.
Honestly this was SO frustrating I almost had to go through the entire collection of answers here to find out that most of them weren't doing what I wanted. In fact I only wanted to display to the browser the following:
- The last row ever created on my table
- Just 1 resource
I wasn't looking to ordering a set of resources and order that list through in a descending fashion, the below line of code was what worked for me on a Laravel 8 project.
Model::latest()->limit(1)->get();