Select the last row in the table
P粉986028039
P粉986028039 2023-10-11 09:04:14
0
2
527

I want to retrieve the last file inserted into my table. I know the method first() exists and gives you the first file in the table, but I don't know how to get the last insert.

P粉986028039
P粉986028039

reply all(2)
P粉020085599

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

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

This way you won't retrieve all records. Better shortcut for orderBy.

P粉933003350

You need to sort by the same fields as you are sorting now, but in descending order. For example, if you have a timestamp called upload_time when the upload is complete, you can do the following;

For versions prior to Laravel 4

return DB::table('files')->order_by('upload_time', 'desc')->first();

For Laravel 4 and above

return DB::table('files')->orderBy('upload_time', 'desc')->first();

For Laravel 5.7 and above

return DB::table('files')->latest('upload_time')->first();

This will sort the rows in the file table by upload time, descending , and take the first one. This will be the most recently uploaded file.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template