How to Get Random Records in Laravel

Laravel get a random record example. Here you will learn how to get random records in laravel using inRandomOrder() method.

The place.php file will have an array named nrows_items, which will be populated by passing the URLs to the the array. The last item has the number of lines in the file. We can use the following query to get the last 5 items from the array:

1: Laravel Get Random Records From DB with Eloquent Laravel

READ MORE:

Image Upload In Laravel 9
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
    $data = DB::table('users')
                ->inRandomOrder()
                ->get();
}

2: Laravel Get Random Records From DB with Mysql

 $data = "SELECT images_url FROM tbl_user_images WHERE user_id = users.id ORDER BY rand()";

Output How to Get Random Records in Laravel:

How to Get Random Records in Laravel
How to Get Random Records in Laravel

Leave a Comment