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
Laravel 11 Simple Pagination Example
Laravel 11 Simple Pagination E...

Hello developers! In this article, we'll see the laravel 11 simple pagination example. Here, we'll use Bootstrap...

Read More

May-03-2024

Cron Job Scheduling In Laravel
Cron Job Scheduling In Laravel

In this article, we will see cron job scheduling in laravel. Many times we require to run some piece of code in a specif...

Read More

Sep-28-2020

Razorpay Payment Gateway Integration in Laravel 10 and VueJS 3
Razorpay Payment Gateway Integ...

In the rapidly advancing world of technology, the combination of Laravel 10 and VueJS 3 is unparalleled and exceptionall...

Read More

Aug-30-2023

Laravel 8 Google Pie Chart Example
Laravel 8 Google Pie Chart Exa...

This article will show the laravel 8 google pie chart example. Google charts use to visualize data on you...

Read More

Mar-03-2021