Select the last row in the table

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

+123
eloquent laravel
May 14 '13 at 17:38
source share
10 answers

You will need to sort by the same field that you are currently sorting, but in descending order. For example, if you had a timestamp when the download was done, called upload_time , you would do something like this;

For Pre-Laravel 4

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

For Laravel 4 onwards

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

For Laravel 5.7 and higher

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

This will sort the lines in the file table by load time, in descending order, and take the first one. This will be the last downloaded file.

+175
May 14 '13 at 17:49
source share

Use the last area provided by Laravel out of the box.

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

This way you do not retrieve all the records. More convenient shortcut for orderBy.

+85
Apr 16 '17 at 5:59 on
source share

You never mentioned whether you use Eloquent, Laravel's default ORM or not. In case you want, say that you want to get the last record of the user table created by_at, you could probably do the following:

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

First, he orders a user named created_at, in descending order, and then takes the first record of the result.

This will return you an instance of the User object, not a collection. Of course, to use this alternative, you must have a User model extending the Eloquent class. This may seem a bit confusing, but it’s very easy to get started, and ORM can be really useful.

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

+71
Mar 24 '14 at 4:33
source share

To get the latest recording details

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

To get the last record ID

  1. Model::all()->last()->id; or
  2. Model ::orderBy('id', 'desc')->first()->id;
+33
07 Oct '15 at 13:02
source share

Laravel collections have a last method

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

This is the best way to do this.

+13
Sep 10 '15 at 16:24
source share

Try this:

 Model::latest()->get(); 
+12
May 22, '17 at 19:13
source share

To get the latest record data, use the following code:

 Model::where('field', 'value')->get()->last() 
+3
Apr 19 '17 at 11:13 on
source share

If you are looking for the actual string that you just inserted with Laravel 3 and 4 when you execute the save or create action on the new model, for example:

 $user->save(); 

-or -

 $user = User::create(array('email' => 'example@gmail.com')); 

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

Finding the latest inserted records in a low volume system will work almost all the time, but if you ever need to insert an insert, you can go looking for the wrong record. This can actually be a problem in a transactional system where multiple tables need to be updated.

+1
May 14 '13 at 19:19
source share

Somehow all of the above does not seem to work for me in laravel 5.3, so I solved my problem using:

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

I hope I can help someone out.

+1
Jan 01 '16 at 2:40
source share

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

0
Dec 25 '18 at 3:19
source share



All Articles