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.
1)first()
2)orderBy()
3)latest()
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 ?
- How to Open URL in New Tab using Jquery?
- Laravel 7.x and 6.x Routing Tutorial
- Ng Bootstrap Modal in Angular 8 Example
- Bootstrap 4 Datepicker in Angular 9/8 Example
- Disable Registration Route in Laravel
- Bootstrap Timepicker in Angular Example
- Count Number of Pages in PDF - PHP Script
- Digital Signature PHP Script Example