In this article, we'll see how to get random records in laravel 10. Here, we'll learn about getting random records in laravel 10 using the inRandomOrder() function. Here you will learn how to get random records from DB in laravel using the inRandomOrder() method.
In this guide, you'll learn how to use the inRandomOrder()
method to select random data from your database.
In this Laravel example, the index
the function retrieves 7 random records from the 'posts' table using the inRandomOrder()
method, making it easy to display dynamic content on your website.
public function index()
{
$data = DB::table('posts')
->inRandomOrder()
->limit(7)
->get();
}
In this Laravel code snippet, the index
the function retrieves 5 random records from the 'posts' table using Eloquent's inRandomOrder()
method, ideal for displaying diverse and engaging content on your website.
public function index()
{
$data = Post::inRandomOrder()
->limit(5)
->get();
}
You might also like:
- Read Also: Laravel 10 REST API CRUD Operation
- Read Also: How to Store Image in Storage Folder in Laravel 10
- Read Also: Laravel 10 REST API Authentication using Sanctum
- Read Also: How to Create Multiple Database Connections in Laravel 10