In L-4 it was simple:
$random_quote = Quotation::all()->random(1); But now in L-5 not a single method described in this post is working: Laravel - Eloquent or Fluent random row
My view file just gets blank.
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();
random() gives error in 5.2, so instead u can use inRandomOrder https://laravel.com/docs/5.2/queries#ordering-grouping-limit-and-offset ,
and it works on Eloquent like
Model::inRandomOrder()->first()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With