ItSolutionStuff.com

How to Get Last 10 Records in Laravel?

By Hardik Savani • April 16, 2024
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, laravel 9, laravel 10 and laravel 11 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: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Get Request Header in Laravel?

Read Now →

Laravel - How to Get All Files in a Directory?

Read Now →

How to Get All Routes in Laravel?

Read Now →

How to Write Text on Existing PDF File in Laravel?

Read Now →

How to Convert JSON to Array in Laravel?

Read Now →

How to Convert Number to Words in Laravel?

Read Now →

Laravel Eloquent doesntHave() Condition Example

Read Now →

Laravel Eloquent whereHas() Condition Example

Read Now →

Laravel Case Sensitive Query Search Example

Read Now →

Delete All Records from Table in Laravel Eloquent

Read Now →

Laravel Eloquent whereNotNull() Query Example

Read Now →

Laravel One to Many Eloquent Relationship Tutorial

Read Now →

Laravel Many to Many Eloquent Relationship Tutorial

Read Now →