how get random row laravel-5

These works but probably you didn’t use the right namespace, just use the use statement at the top of your class name like this:

<?php namespace SomeNamespace;

use App\Quotation; // Says "Quotation.php" is in "App" folder (By default in L-5.0)

class someClass {
    //...
}

Then you may use in your method something like this:

// You may add: use DB; at the top to use DB instead of \DB
$random_quote = Quotation::orderBy(\DB::raw('RAND()'))->first();

Or this:

$random_quote = Quotation::orderByRaw("RAND()")->first();

Update (Since Laravel – 5.2):

$random_quote = Quotation::inRandomOrder()->first();

Leave a Comment