How to get Random Records in Laravel?
Teams applying “How to get Random Records in Laravel?” in a production project can use this workflow reference to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Sep 28, 2022
Hello Friends,
In this tutorial, we will go over the demonstration of how to get random records in laravel. you can see that laravel get random record from the model example. it's a simple example of laravel's eloquent get random record code example. This post will give you a simple example of laravel's eloquent inrandomorder() method example. Let's see the below example laravel get retrieves random records.
You must be visiting many blogs. So sometimes you have to see these blogs in the sidebar. It is written in random posts and there are some posts below it.
If you are building a blog application in the laravel framework. And want to get random posts/data from the database in laravel. And want to display this random data anywhere on your laravel blog. So at that time, you need to use inRandomOrder() method to get random records in laravel.
This article will guide you on how to use get random records from the database in laravel using inRandomOrder() method. As well as get random records from a collection in laravel.
You can use this example with the versions of laravel 6, laravel 7, laravel 8, and laravel 9.
You can see bellow default route for login post method:
Step 1: Install Laravel
first of all we need to get a fresh Laravel version application using the bellow command, So open your terminal OR command prompt and run the bellow command:
composer create-project laravel/laravel example-app
Step 2: create RandomrecordController we will create RandomrecordController with inRandomOrder() method. so you can see the below code with output:
php artisan make:controller RandomrecordController
App\Http\Controllers\RandomrecordController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class RandomrecordController extends Controller
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
$data = DB::table('posts')
->select('posts.id','posts.name','posts.title')
->inRandomOrder()
->limit(5)
->get();
dd($data->all());
}
}
Step 3: Create Random Route
web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\RandomrecordController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('random', [RandomrecordController::class, 'index']);
Start Development Server
Start the development server. Use the PHP artisan serve command and start your server:
php artisan serve
Now you are ready to run our example so run the below command to quick run.
http://localhost:8000/random
It will help you...
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial