How To Get Last Record In Laravel 8

Websolutionstuff | Jan-26-2022 | Categories : Laravel PHP MySQL

In this example, we will see how to get the last record in laravel 8. You can simply get the last record using laravel 8 eloquent model. In PHP, you can use order by clause with descending order to get the last record from the database table.

In laravel provides the latest() method to get the last record from the database. And in MySQL get the last record using the ORDER BY clause with desc.

So, let's see laravel 8 get last record, how to get latest record in laravel, how to get last record in mysql, get last updated record in laravel 8.

MySQL Syntax : 

You can see the MySQL syntax like below. 

SELECT column_name FROM table_name  
ORDER BY column_name DESC  
LIMIT 1;

Example :

select *from users ORDER BY id DESC LIMIT 1;

 

 

Laravel Syntax :

Example 1:

$user = DB::table('users')
                ->latest()
                ->first();

Example 2 :

$user = User::orderBy('id', 'DESC')->first();

 


You might also like :

Recommended Post
Featured Post
How To Generate QR Code In Angular 13
How To Generate QR Code In Ang...

In this article, we will see how to generate QR code in angular 13. In this example, we will use the angularx-qrcod...

Read More

Jun-09-2022

How To Add Toastr Notification In Laravel
How To Add Toastr Notification...

In this tutorial, I will show you how to add toastr notification in laravel application. There are many types of no...

Read More

May-29-2020

Laravel 11 Create CRUD Operation with Database
Laravel 11 Create CRUD Operati...

Hello developers! In this article, we'll learn about Laravel 11 to create Creat Reas Update Delete operations w...

Read More

Apr-08-2024

How to Install Select2 in Laravel 10 Example
How to Install Select2 in Lara...

Hey there, Laravel enthusiasts! If you're looking to add a touch of elegance and user-friendliness to your web...

Read More

Feb-14-2024