How to Get Last Record from Database in Laravel?
Hey Guys,
In this tutorial, I will show you how to get last record in laravel query. you will learn how to get last record in laravel. we will help you to give an example of how to get latest created record in laravel. In this article, we will implement a laravel get last record.
Sometimes we may require to get only last record of table in our project, we can get several way. We can fetch last record of database table using latest() or orderBy(). In bellow example you can see how i get then last record of table.
here is a simple example of get last record from tabel in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 project.
Example 1: Laravel Query Get Last Record Using latest() & first()
I have "items" table and i want to get last record of "items" table. In first example i use latest() with first() because latest() will fetch only latest record according to created_at then first() will get only single record:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Item;
class ItemController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$last = Item::latest()->first();
dd($last);
}
}
Example 2: Laravel Query Get Last Record Using orderBy() & first()
Now, In this example i used orderBy() that belong to id, it will always return max id record:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Item;
class ItemController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$last = Item::orderBy('id', 'DESC')->first();
dd($last);
}
}
Example 3: Laravel Query Get Last Record Using latest('id') & first()
In this example latest() with argument, latest() will take created_at by default, if you want to give id then you can give argument as field name.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Item;
class ItemController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$last = Item::latest('id')->first();
dd($last);
}
}
But If you want to get last inserted id then you have to follow this link :
How to get last inserted id in laravel 5?.
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Eloquent Group By Year with Sum Example
- Laravel Eloquent Sum Multiple Columns Example
- Laravel Eloquent firstOrCreate Example
- Delete All Records from Table in Laravel Eloquent
- Laravel Eloquent whereNotNull() Query Example
- Laravel Eloquent whereNotBetween() Query Example
- Laravel Eloquent selectRaw() Query Example
- Laravel Eloquent whereRaw Condition Example
- Laravel Collection Merge | How to Merge Two Eloquent Collection?
- Laravel Many to Many Eloquent Relationship Tutorial
- Laravel - How to Check Column Exists or Not in Table?
- How to Get Last Inserted Id in Laravel?
- Laravel Create JSON File & Download From Text Example