How to randomly get the user id from users table in laravel

Created at 30-Jan-2024 , By samar

In this article, we will learn to get the random id from users table using inRandomOrder() method and random() method in Laravel. It enables the random insertion of user ID values into the user_id column within other tables. You can use it to seed database table and other cases in which you want to get the random user id from users table in Laravel.

  • Get random user id value from users table using inRandomOrder() method

    \App\Models\User::inRandomOrder()->first()->id
    
  • Get the random user id from users table using random() method

    \App\Models\User::all()->random()->id
    

Back to code snippet queries related laravel

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.