Laravel’s Query Builder has a method get() to actually get the rows, but what if we want to get random rows? Like, for example, we have 30 questions in the database, and want to show 10 random ones?
The bad news is there is no method ->random() instead of get() or something like that. The good news is there is a simple solution, which is actually not so much Laravel, but more directly MySQL-related. The thing is – you can order by random in most of the databases. Here’s MySQL example:
SELECT * FROM questions ORDER BY RAND() LIMIT 10
And in Laravel we can just use Raw expressions:
$questions = Question::orderBy(DB::raw('RAND()'))->take(10)->get();
Or even shorter:
$questions = Question::orderByRaw('RAND()')->take(10)->get();
Just a quick tip this time!
I needed to do just this yesterday but I went a non-SQL route.
$records = Record::all();
$rand = rand(0, $records->count() – 1);
$randomRecord = $records->get($rand);
return $randomRecord;
Thanks for comment Christopher. I wouldn’t do it this way, cause ::all() takes all the records from the DB, what if it’s millions of records? And also it’s a couple of lines of code instead of one. On the other hand your code is non MySQL specific, can work for other DBs.
If you really want to do it that way (not that fun if you have millions of rows) the Collection class has a random method http://laravel.com/api/5.1/Illuminate/Support/Collection.html#method_random
Random::all()->random(10);
If you have a million records:
$rand = rand(0, Record::count() – 1);
$randomRecord = $records->get($rand);
I like orderByRaw(‘RAND()’) way how to do this.
Christopher Lamm —
In this way I recommend straight fetch count instead of all record.
$rand = rand(0, Record::count() – 1);
$randomRecord = $records->get($rand);
return $randomRecord;
This really shouldn’t be the way to go with, if you working with big amount of data. It’s the simplest and the easiest way, but also the most slowest. You can read more about it there http://mysql.rjweb.org/doc.php/random
use Illuminate\Support\Collection;
$howMany = 1;
$all = new Collection(App\User::all()); (or ::where(field, value))
$random = $all->random($howMany);
To take a limited set of records in your result set you can use the Laravel collections function TAKE and LIMIT. Follow the bellow link:
http://arunkp.in/pagination-through-api-laravel/
It’s better to first take randomly a certain set of values and then get the resulted set “Model::orderByRaw(‘RAND()’)->take($size)->get()” than to get all column records and then sort a set within this “Model::all()->random($size)”.
Thumbs up! @Povilas Korop
performance of RAND() : very slow on huge database 10k records above
Other concept to pick random rows :
1. Get max id of the table
2. Use php to generate random id , out it in a list. Example : (1,4,5,3,5,3,2) .In PHP we use rand(1,$max_id)
3. Loop through the list and query the data
4. Verify if that id is exist, if not, repick another id. maybe the next record of it)
Old thread but still high up on a search. Since Laravel 5.3 there is:
$pickRandomModels = Model::inRandomOrder()->take(10)->get();
Thank you for this, man!