In this example, I will tell you how to select last row from database table in Laravel with example.
There are several way to get last record from table in Laravel.
In PHP, you normally use order by clause with descending order to get last one record from database table and same you can do in laravel but laravel has some awesome method to interact with database table.
You can use
latest method to get last record from database table in Laravel application.
- $last_row=DB::table('products')->orderBy('id', 'DESC')->first();
By running above command, record will be in descending order and you will get first row from record set of table.Get last record from table using latest method
- $last_row = DB::table('products')->latest()->first();
By running above command, you will get last inserted record according to created_at column.