How to Get Last 10 Records in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Get Request Header in Laravel?
- Laravel - How to Get All Files in a Directory?
- How to Get All Routes in Laravel?
- How to Write Text on Existing PDF File in Laravel?
- How to Convert JSON to Array in Laravel?
- How to Convert Number to Words in Laravel?
- Laravel Eloquent doesntHave() Condition Example
- Laravel Eloquent whereHas() Condition Example
- Laravel Case Sensitive Query Search Example
- Delete All Records from Table in Laravel Eloquent
- Laravel Eloquent whereNotNull() Query Example
- Laravel One to Many Eloquent Relationship Tutorial
- Laravel Many to Many Eloquent Relationship Tutorial