In this example, I will show how to get random value from array in laravel. Laravel Arr::random() function is use to get random value from array.
Example : Get random value from Array
<?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 how to get random value from array and it can help you…