This post will demonstrate how to retrieve the most recent 10 records using Laravel. You can comprehend how to use Laravel to acquire the most recent 10 records. You can comprehend how to use Laravel to fetch the last 10 records. You will discover how to access Laravel  last n records. To access the last 5 records in Laravel, follow the steps below.

This example works with versions of Laravel 6, Laravel 7, Laravel 8, Laravel 9, and Laravel 10.

I will assist you if you want to get the most recent n records from a database in Laravel. I'll offer you two straightforward examples. To retrieve the most recent 10 records from the database in Laravel, we'll utilize the latest(), orderBy(), and take() eloquent methods.


Latest Method :


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Article;

class ArticleController extends Controller

{

public function index(Request $request)

{

#Lastest method used for last records

#take method used for number of records need

$articles = Article::latest()->take(10)->get();

return $articles;

}

}


Order By Method :


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Article;

class ArticleController extends Controller

{

public function index(Request $request)

{

$articles = Article::orderBy("id", "DESC")->take(10)->get();

return $articles;

}

}

Share