Laravel Arr random() function Example

In this example, I will explain laravel arr random() function. Laravel array random() function is use for getting random value from array. We will show to get random value from array in laravel application.

Also i will explain how to get random number from array in laravel using laravel arr random() function.

Read Also :  Laravel 8 Resource Route Controller Example

Example : How to get random number from array in laravel

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Arr;

class UserController extends Controller
{
    public function index(){        
        $array1 = [10,20,30,40,50,60];

        $randomed1 = Arr::random($array1);
        print_r($randomed1);

        echo '<br>';
        $array2 = ['php', 'laravel', 'html','css'];

        $randomed2 = Arr::random($array2);
        print_r($randomed2);

    }
}

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;

Route::get('users', [UserController::class,'index']);

I hope you understand laravel arr random function or how to get random value from array in laravel and it can help you…