Reputation: 942
I've got a Laravel model, (Cars). I'd like to get 4 random cars, but only picking these 4 random cars from a subset of the top 10 cars, as ranked by their 'score_total'
I've tried the code below, but the inRandomOrder doesn't seem to have any effect. Any advice would be much appreciated. I'm on Laravel 5.4.
Cars::orderBy('score_total','DESC')->take(10)->inRandomOrder()->take(4)->get();
Upvotes: 1
Views: 68
Reputation: 845
Try to use random
instead :
$cars = Cars::orderBy('score_total','DESC')->take(10)->get();
$random = $cars->random(4);
Upvotes: 2