gpt4 book ai didi

php - 从 avg() 函数下订单

转载 作者:行者123 更新时间:2023-11-29 19:03:26 28 4
gpt4 key购买 nike

我目前正在开发一款 Laravel 应用程序,我希望在这一部分中显示评分最高的书籍。我将我的评级存储在评级模型中。我的想法是这样的,但是,确实行不通。如果有人正确回答我,我会非常非常高兴。

$book = Book::all();
$ratings = Rating::where('book_id', '=', $book->id)->orderBy(avg('rating'));
return view('toprated')->withRatings($ratings);

toprate.blade.php

@foreach($ratings as $rating)
{{ $rating }}
@endforeach

最佳答案

如果需要使用MYSQL函数,请使用orderByRaw:

$ratings = Rating::where('book_id', '=', $book->id)->orderByRaw("avg(rating)");

关于php - 从 avg() 函数下订单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43690832/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com