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 ?
- Multidimensional Array Search By Value in PHP
- Laravel Install Font Awesome Icons Example
- How to Use Moment JS in Laravel?
- Angular 9 Get Environment Variables Example
- Angular Delete a Component From Cli Example
- How to Upload File from Local to Server using SSH?
- Angular Use CKEditor Example Tutorial
- How to Use Multiple Select Dropdown in Laravel?