How to get or select last row from the Table in laravel
Today, We will tell you about how to get or select the last row from a table in Laravel. Laravel is a PHP framework. it provides many eloquent method library such as first(), latest() and orderBy(). let’s see that talking about how to select or get the last row from the table.
Generally, When we need to check the login credentials to the table at that time we have been used that. There are several ways to get or select a row from the table. here we are using first(), latest() and oderBy methods to select a row from the table.
Using The latest() Method
The latest() method get the latest row of the created_at date wise from the table.
$lastrow = DB::table('students')->latest()->first();
You can pass the table field id into the latest() method and get the last row.
$lastrow = DB::table('students')->latest('id')->first();
Using The orderBy Method
here below example in we will get the latest row using the orderBy descending order with the first() method.
$lastrow = DB::table('students')->orderBy('id', 'DESC')->first();