How to get last record of database table in Laravel?

Today, We want to share with you How to get last record of database table in Laravel?.In this post we will show you laravel 6 get last record example, hear for get last record laravel 6 we will give you demo and example for implement.In this post, we will learn about Get the last record from database table with an example.

How to get last record of database table in Laravel?

There are the Following The simple About How to Fetch last record of database table in Laravel6? Full Information With Example and source code.

Read Also:  How to display image from storage path using route in Laravel?

As I will cover this Post with live Working example to develop Get the last record from database table, so the some major files and Directory structures for this example is following below.

Using latest() belong to created_at field:

$product_last = DB::table('products')->latest()->first();

Using orderBy() belong to id field:

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

Using latest() belong to id field:

$product_last3 = DB::table('products')->latest('id')->first();

Select Last Row in the Table

return DB::table('products')->latest('updated_at')->first();

using DB Object

DB::table('products')->get()->last();

To Retrive last record details

Product ::all()->last(); or
Product ::orderBy('id', 'desc')->first();

To Retrive last record id

Read Also:  Laravel 6 Localization tutorial example - PHP

Product::all()->last()->id; or
Product ::orderBy('id', 'desc')->first()->id;

Use the latest scope provided by Laravel out of the box.

Product::latest()->first();

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel 6 latest first example.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.