【发布时间】:2020-12-26 13:00:30
【问题描述】:
我想显示所有产品的平均评分。在这种情况下,我有两个表,例如 Product Table 和 Product Rating Table
例如:
我的产品表
ProductID | ProductName
-------------------------
1 | ABC Product
2 | XYZ Product
3 | LMN Product
我的评分表
ID | ProductID | Value
-------------------------
1 | 1 | 4
2 | 1 | 5
3 | 2 | 3
我需要这样的输出,
[
{
"ProductID": 1,
"ProductName": ABC Product,
"AverageRating": 4.5,
},
{
"ProductID": 2,
"ProductName": XYZ Product,
"AverageRating": 3.0,
},
{
"ProductID": 3,
"ProductName": LMN Product,
"AverageRating": 0,
}
]
如何在 laravel 中获得此输出?提前致谢!
【问题讨论】:
-
你使用的是什么版本的 Laravel?
-
Laravel 框架 7.16.1
标签: php laravel laravel-5 eloquent laravel-4