Posts Learn Components Snippets Categories Tags Tools About

How to Get a Random Row in Laravel Using Eloquent or Fluent

Learn how to get or select a random row in Laravel using eloquent or fluent in Laravel the easy way.

Created on Oct 05, 2021


To randomly get a record in Laravel you can make use of the "inRandomOrder()" method available to all of your model instances.

Using inRandomOrder()

For example, if you want to get a random user you can write the following code and it will return one random user.


Using Collection random() method

The 2nd way is to use the "random()" method of the collection instance and this way will retrieve all of the records and randomly get the record/instance.

Do note you can pass in how much record/instance by passing the number of records to the "random()" method.


Using orderByRaw() method

The 3rd way is to use "orderByRaw()" method and it will accept raw SQL function. If you are using PostgreSQL then the raw SQL function will be "'RANDOM()'"

One other way to write the code above is as follows.


If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️


PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that are right for your project.

View Components