How can I select a random row using Eloquent or Fluent in the Laravel framework?

I know that by using SQL, you can do order by RAND(). However, I would like to get the random row without doing a count on the number of records prior to the initial query.



Laravel >= 5.2:


or to get the specific number of records

// 5 indicates the number of records User::inRandomOrder()->limit(5)->get(); // get one random record

or using the random method for collections:

User::all()->random(10); // The amount of items you wish to receive

Laravel 4.2.7 - 5.1:


Laravel 4.0 - 4.2.6:


Laravel 3:


Check this article on MySQL random rows. Laravel 5.2 supports this, for older version, there is no better solution then using RAW Queries.

edit 1: As mentioned by Double Gras, orderBy() doesn't allow anything else then ASC or DESC since this change. I updated my answer accordingly.

edit 2: Laravel 5.2 finally implements a wrapper function for this. It's called inRandomOrder().