How To Get Last Record of Database Table in Laravel 8

Share this Article
Reading Time: 3 minutes

Inside this article we will see the concept i.e How to get last record of database table in laravel 8. Article contains classified information about getting last record from database table.

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 8 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.

Learn More –

Let’s get started.

Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project.

If your system doesn’t has composer Installed, Learn Composer Installation Steps.

Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server:

Assuming laravel already installed at system.

Concept: Last Row Data (Laravel 8 Methods)

We have options to get exact the last row from any database table in laravel 8.

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

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

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 8 Tutorial in a very detailed way.

Buy Me a Coffee

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.

Learn Web Development Courses @ $4 only.
Web Development Courses @ $4