Home » How To Get Latest Records In Laravel

How To Get Latest Records In Laravel

Last updated on May 24, 2021 by

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.

Table of Contents
1. Using latest() Method In Laravel
2. Using orderBy() Method In Laravel

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:

  1. Laravel One To One Relationship Tutorial
  2. Best Way to Remove Public from URL in Laravel
  3. How To Print Or Debug Query In Laravel
  4. Specified Key Was Too Long Error In Laravel
  5. AJAX PHP Post Request With Example
  6. How To Use The Laravel Soft Delete
  7. How To Add Laravel Next Prev Pagination
  8. Laravel Remove Package With Example (Correct Way)
  9. Difference Between Factory And Seeders In Laravel
  10. Laravel: Increase Quantity If Product Already Exists In Cart
  11. How To Calculate Age From Birthdate
  12. How to Convert Base64 to Image in PHP
  13. Check If A String Contains A Specific Word In PHP
  14. How To Find Duplicate Records in Database
  15. How To Convert Word To PDF In Laravel
  16. 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!

 
 

Leave a Comment