HDTuto.com

How to retrieve random records in Laravel 5.6?


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.

Query:

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

->inRandomOrder()

->get();


dd($randomProduct);

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




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