how get random row laravel-5

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

    orderByRaw('RAND()')
    

    Note: It will take in ram all the rows from the rest of the query, so if you have a large table whithout other filters in the same query it will give bad performances, otherwise this is your option

提交回复
热议问题