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 Install Material Theme in Angular 17
How to Install Material Theme...

In this article, we'll install the material theme in angular 17. Material Theme brings a polished design and us...

Read More

Mar-29-2024

Laravel 9 Yajra Datatable Example
Laravel 9 Yajra Datatable Exam...

In this artical we will see laravel 9 yajra datatable example. As we all used datatable on our backend side project, Her...

Read More

Mar-10-2022

Laravel 11 Livewire Multi Step Wizard Form
Laravel 11 Livewire Multi Step...

In this article, we'll see how to create a multi-step form wizard in laravel 11 Livewire. Here, we'll see step b...

Read More

Jun-17-2024

How To Merge Two PDF Files In Laravel 9
How To Merge Two PDF Files In...

In this article, we will see how to merge two pdf files in laravel 9. Here, we will learn laravel 8/9 to merge two...

Read More

Dec-20-2022