How to retrieve random records in Laravel 7.x and 6.x?

May 23, 2018 | Category : Laravel 5.6 Laravel 5.5 Laravel 5 Laravel PHP

Hi guys,

A few days ago I was working on my new laravel 5.6 application and I require to get random products rows from MySQL database using laravel eloquent. I was thinking to use rand() of mysql in laravel but i checked laravel 5 documentation and i found that laravel provide inRandomOrder().

inRandomOrder() will help to retrieve random rows from database table. we can simple use like use get(), first(), where() etc of laravel eloquent. So i just share my simple example bellow.

Let's see bellow inRandomOrder() query.


$randomProduct = DB::table('products')




You can try this way and let's use it....