Often, developers required to get the latest records in Laravel. There are several ways to do this. But we will first see the short and right way to do it. Let’s just jump into it.
Get Last Inserted Records In Laravel
The followings are the ways to get the last inserted records in the Laravel.
01 Using latest() Method In Laravel
Laravel has introduced the latest() method in version 5.3
. As the name suggests this method is used to get the latest records from the database.
- The latest() method is used
created_at
column to sort the data. - You can also pass the column name if you want to sort the data by that particular column like
latest('username')
Example: Fetch Latest 5 Records In Laravel
Let’s say, we have posts
table in our database and we want to fetch the latest 5 posts then we can do it with latest() method as below:
PostController.php
<?php
namespace App\Http\Controllers;
use App\Models\Post;
class PostController extends Controller
{
public function index()
{
$latestPosts = Post::latest()->take(5)->get();
// take() method is equivalent to limit() method
return view('post.index', [
'posts' => $latestPosts
]);
}
}
In the above example, we have used the latest() method along with take() method which will only fetch the provided numbers of records from the database. The take() method is equivalent to the limit() method of Laravel.
Example: Get Only Last Record In Laravel
To get only the last record in Laravel, you can use the first() method of Laravel along with latest()
method like below:
<?php
Post::latest()->first();
02 Using orderBy() Method In Laravel
The orderBy() method is used to order the result based on the provided column name and order direction either ASC
or DESC
.
Laravel’s latest()
method has also used the orderBy()
method in the background to sort the fetched records. Let’s see it by example.
Example: Fetch Latest 5 Records In Laravel
PostController.php
<?php
namespace App\Http\Controllers;
use App\Models\Post;
class PostController extends Controller
{
public function index()
{
$latestPosts = Post::orderBy('created_at','desc')->take(5)->get();
// take() method is equivalent to limit() method
return view('post.index', [
'posts' => $latestPosts
]);
}
}
Example: Get Only Last Record In Laravel
<?php
Post::orderBy('created_at','desc')->first();
Additionally, read our guide:
- Laravel One To One Relationship Tutorial
- Best Way to Remove Public from URL in Laravel
- How To Print Or Debug Query In Laravel
- Specified Key Was Too Long Error In Laravel
- AJAX PHP Post Request With Example
- How To Use The Laravel Soft Delete
- How To Add Laravel Next Prev Pagination
- Laravel Remove Package With Example (Correct Way)
- Difference Between Factory And Seeders In Laravel
- Laravel: Increase Quantity If Product Already Exists In Cart
- How To Calculate Age From Birthdate
- How to Convert Base64 to Image in PHP
- Check If A String Contains A Specific Word In PHP
- How To Find Duplicate Records in Database
- How To Convert Word To PDF In Laravel
- How To Break Nested Loops In PHP Or Laravel
That’s it for now. We hope this article helped you to get the latest records in Laravel.
Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance. 🙂 Keep Smiling! Happy Coding!