how get random row laravel-5

前端 未结 9 679
花落未央
花落未央 2021-02-02 07:12

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: Lar

9条回答
  •  滥情空心
    2021-02-02 07:30

    In Laravel 5.1 (and Laravel 5.2) there is a random method in the Collection class returned by the Eloquent builder.

    https://laravel.com/docs/5.1/collections#available-methods

    So your call

    $random_quote = Quotation::all()->random(1);
    

    or

    $random_quote = Quotation::where('column', 'value')->get()->random(1);
    

    should work properly.

提交回复
热议问题