How to Get Last 10 Records in Laravel?

By Hardik Savani September 28, 2022 Category : Laravel

Hello,

This article will provide an example of how to get last 10 records in laravel. you can understand a concept of get latest 10 records laravel. you can understand a concept of laravel get last 10 records. you will learn how to get last n numbers records in laravel. follow the below step for laravel get last 5 records.

You can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 versions.

If you want to get last n number of records from database in laravel then i will help you with it. i will give you simple two examples. we will use latest() and orderBy() with take() eloquent method to get last 10 records from database in laravel.

So, let's see the below examples:

Example 1:

app/Http/Controllers/PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$posts = Post::latest()->take(10)->get();

dd($posts);

}

}

Example 2:

app/Http/Controllers/PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

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

dd($posts);

}

}

Output:

I hope it can help you...

Tags :