How To Get Last Record of Database Table in Laravel 10

Reading Time: 3 minutes
363 Views

This Laravel 10 article explains how to retrieve the last record in a database table. As a Laravel developer, you frequently encounter situations in which you need to retrieve the most recent entry from a database table. This tutorial will walk you through the steps necessary to complete this operation quickly using Laravel 10.

In this post you will see the concept of How To Get Last Record of Database Table in Laravel 10.

Laravel provides several option to get the last or we can say the latest row of table. We will assume that we have a table and from that we want the last inserted row using laravel 10 methods.

If you are looking for an article which gives you understanding about getting last row data from database table then this article is best for you.

Read More: Inertia Js Authentication in Laravel 10 with Jetstream Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Methods: Get Last Row Data

We have options to get exact the last row from any database table in laravel are:

  • Using latest() Method
  • Using orderBy() Method

Suppose we have a students table in which 100 rows are there.

Read More: Laravel 10 Authentication with Laravel UI Tutorial

Using latest() Method

latest() method get the last row inserted from a table. It works on the basis of created_at timestamp column.

We use latest() method with first() to get the single row data.

Query #1: Get last row from students table on the basis of created_at column

$last_row = DB::table('students')->latest()->first();

Query #2: Get last row from students table on the basis of id column

$last_row = DB::table('students')->latest("id")->first();

Using orderBy() Method

Second method we have using orderBy(). On the basis of descending order of id value, we can get the last row from table.

$last_row = DB::table('students')->orderBy('id', 'DESC')->first();

We hope this article helped you to learn How To Get Last Record of Database Table in Laravel 10 in a very detailed way.

Read More: Use Laravel 10 To Get Files Information From Directory

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more