How to get last record from table in PHP Laravel Framework?

How to get last record from table in PHP Laravel Framework?

In this example, I will tell you how to select last row from database table in Laravel with example.

There are several way to get last record from table in Laravel.

In PHP, you normally use order by clause with descending order to get last one record from database table and same you can do in laravel but laravel has some awesome method to interact with database table.

You can use latest method to get last record from database table in Laravel application.

Get last record from table using orderBy clause
  1. $last_row=DB::table('products')->orderBy('id', 'DESC')->first();

By running above command, record will be in descending order and you will get first row from record set of table.

Get last record from table using latest method
  1. $last_row = DB::table('products')->latest()->first();

By running above command, you will get last inserted record according to created_at column.

Phone: (+91) 8800417876
Noida, 201301
Attention Required! | Cloudflare

Sorry, you have been blocked

You are unable to access ressim.net

Why have I been blocked?

This website is using a security service to protect itself from online attacks. The action you just performed triggered the security solution. There are several actions that could trigger this block including submitting a certain word or phrase, a SQL command or malformed data.

What can I do to resolve this?

You can email the site owner to let them know you were blocked. Please include what you were doing when this page came up and the Cloudflare Ray ID found at the bottom of this page.