Laravel Order由随机 [英] Laravel OrderBy Random
本文介绍了Laravel Order由随机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
作为Laravel的新手,我正在尝试随机显示画廊的图像.在 routes.php 中,我目前有以下代码:
As a novice in Laravel, i'm trying to display the images of a gallery randomly. In routes.php, I currently have this code:
// Get galleries
$galleries = App\Gallery::orderBy('id', 'DESC')->get();
您有什么想法可以使其正常工作吗?
Do you have any idea to make it work?
谢谢
推荐答案
For Laravel >= 5.2 you could use inRandomOrder()
method.
说明:
inRandomOrder()
方法可用于对查询结果进行随机排序.例如,您可以使用此方法来获取随机用户:
Description : The
inRandomOrder()
method may be used to sort the query results randomly. For example, you may use this method to fetch a random user:
示例:
$galleries = App\Gallery::inRandomOrder()->get();
//Or
DB::table('gallery')->inRandomOrder()->get();
对于其他版本> = 5.0,您可以使用 random()
方法.
说明:
random()
方法从集合中返回一个随机项.
Description : The
random()
method returns a random item from the collection.
示例:
App\Gallery::all()->random()->get();
希望这会有所帮助.
这篇关于Laravel Order由随机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文