If you want to select a random row using eloquent in Laravel , here is how to do it in different versions of Laravel.
Laravel 5.2 and above
User::inRandomOrder()->get();
Laravel 4.2.7 to 5.1
User::orderByRaw("RAND()")->get();
Laravel 4.0 to 4.2.6
User::orderBy(DB::raw('RAND()'))->get();
For PostgreSQL you can use RANDOM().
It is possible to paginate the results but the sorting will also be random, which has no use.
You can change the argument in the random function to get more than one data but it is not possible to perform this on huge data.
$model=Model::all()->random(1);
If you have any query feel free to comment.
One Comment