inRandomOrder() method with example in laravel

Updated at 22-Aug-2021, By samar

inRandomOrder() method with example in laravel

With this article, we’ll look at some examples of how to address the "inRandomOrder() method with example in laravel" problem.

You can call inRandomOrder() method to get the records from the table in random order. Here we provide real example code and step-by-step implementation of the code in your project.
  • Get records from user table in random order using inRandomOrder() in laravel

    $users = User::inRandomOrder()->get();
    //Step by step code implementation
    use App\Http\Controllers\HomeController;
    Route::get('/get-data-in-random-order', [HomeController::class, 'getDataInRandomOrder']);
    //Create HomeController using artisan command
    php artisan make:controller HomeController
    //HomeController with method
    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use App\Models\User;
    class HomeController extends Controller
        public function getDataInRandomOrder(){
            $users = User::select('id', 'name')->inRandomOrder()->get();
            echo "<pre>";

    You can get all the records from the user table in Laravel in random order. This code snippet will return all the records in a different order on each reload of the page.

Back to code snippet queries related laravel