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....
Do you like below Tutorials ?
- How to create pdf file in Laravel 7.x and 6.x?
- How to resize image while uploading in Laravel 7.x and 6.x?
- Ajax CRUD in Laravel 7.x and 6.x Example
- Multiple File Upload in Laravel 7.x and 6.x Example
- Jquery Ajax Post Request in Laravel 7.x and 6.x
- Pagination Example in Laravel 7.x and 6.x
- CORS Middleware in Laravel 6
- Laravel 7.x and 6.x Yajra Datatables Example