Getting random rows from database in a eloquent way


Eloquent has given us many helpful methods to make our development easier and faster. Whenever we need to retrieve data from database, we use get(), first(), all() and many other methods. Today we’re going to talk about getting random data from database.

Getting Started

Before laravel 5.3 , we used to write following syntax probably


Or using a scope :

All these approaches work well. However, laravel provides a method called inRandomOrder() .

The above syntax will retrieve a random row from the database. Easy right! 🙂

Doing these stuffs, you must not forget to limit your data while retrieving data. Without limiting data , it will work fine until you have a million rows of data or more than that then it will effect the performance of your website.


We use take() functions to limit the number of results returned from the query.

Happy coding 🙂

Sharing is caring!

Related Post