Laravel Eloquent inRandomOrder() Query Example

Laravel Eloquent inRandomOrder() Method Example; In this tutorial you will learn how to use inRandomOrder query in laravel application. The inRandomOrder method may be used to sort the query results randomly. For example, you may use this method to fetch a random user or multiple users.

inRandomOrder() will help you to get random records from database table. Let’s see the best examples to get rendom order recrods from database in laravel.

Example 1:

use App\Models\User;

class UserController extends Controller
{
    public function index()
    {
        $randomUsers = User::inRandomOrder()->get();
    }
}

Example 2:

use App\Models\User;

class UserController extends Controller
{
    public function index()
    {
        $users = User::select("*")
                    ->orderBy(DB::raw('RAND()'))
                    ->get();

        dd($users);
    }
}

Hope this will help you..

Leave a Comment