How to Get Last Record of Database in Laravel?
Here I can show you how to Get the Last Record of Database in Laravel with example. you can use the latest() to get the last record from a database table.
I have a student table and I want to get the last record of the student table. For this example, I use the latest() with first() because the latest() will fetch only the latest record according to the last insert recode.
Example :
For Laravel 5.7+ return DB::table('student')->latest('created_at')->first(); ===OR=== return DB::table('student')->latest()->first(); /* output */ stdClass Object ( [id] => 5 [student_name] => joi [age] => 22 [created_at] => 2020-04-01 11:30:36 [updated_at] => 2020-04-01 11:31:26 )
This will order the rows in the student table by create time, descending order, and take the first one. this will be the latest uploaded student.