Here, i will show you how to select last row of database table using Laravel query builder. In PHP Laravel framework, there are several eloquent method to get last record from table.
In this article, I will show you two way to getting last row of the table. In this example i going to use following method of laravel eloquent.
Above three method through we can simply select last record of any table of database, here i gave you "posts" table for example. As bellow you can see first way using "order by".
Using Order By:
$lastRecord = DB::table('posts')->orderBy('id', 'DESC')->first();
now as bellow, i will get last record using latest() method:
Using latest method:
$lastRecord = DB::table('posts')->latest()->first();
As above example will help you to fetch last record of table.
Do you like below Tutorials ?
- Laravel generate random unique string example
- Laravel - chmod(storage/oauth-private.key): Operation failed: Operation not permitted
- Laravel 7.x and 6.x datatables example from scratch
- How to compress PNG image using pngquant in PHP?
- Laravel 7.x and 6.x Chart example using Charts Package
- Guzzle http client GET and POST request example in Laravel 5
- Laravel schema default current timestamp value example
- Laravel 7.x and 6.x Get Site URL Examples