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();
你有什么办法让它发挥作用吗?
谢谢
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();
你有什么办法让它发挥作用吗?
谢谢
For Laravel >= 5.2 you could use inRandomOrder()
method.
可以使用
inRandomOrder()
方法对查询结果进行随机排序.例如,您可以使用此方法获取随机用户:
Example :
$galleries = App\Gallery::inRandomOrder()->get();
//Or
DB::table('gallery')->inRandomOrder()->get();
For other versions >= 5.0 you could use random()
method.
Description : The
random()
method returns a random item from the collection.
Example :
App\Gallery::all()->random()->get();
Hope this helps.