How to sort the results of models randomly in Laravel

Categorized as Laravel Tagged

To someone who didn’t know, there is a pretty and short Eloquent method to use on your model results to get things in random order. See the code below:

use App\Models\Post;

return Post::inRandomOrder()->get();

From the official documentation:

$randomUser = DB::table('users')
                ->inRandomOrder()
                ->first();

Leave a reply

Your email address will not be published.