how get random row laravel-5

前端 未结 9 813
花落未央
花落未央 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:22

    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()
    

提交回复
热议问题