- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想计算嵌套变形关系列的平均值。
模型功能
public function trader_ratings()
{
return $this->morphMany(TraderRatings::class, 'rateable')
->select('rateable_id', 'rateable_type', 'rating')
->avg('rating');
}
延迟加载 Controller
$user = auth('api')->user();
$user_id = $user->id;
$customer_classes = CustomerClassBooking::with([
'trader_class',
'trader_class.trader_ratings',
'vendor',
])
->where('customer_id', $user_id)
->where('status', 'Accepted-paid')
->get();
它不计算
avg
但给出错误。
最佳答案
因为 with()
需要得到关系。 addEagerConstraints来自源代码。当您使用预先加载时,您的关系构建器将调用 addEagerConstraints
.
但是,您的关系方法是返回字符串( avg()
的结果)而不是变形关系。所以出现错误。
您可以更改您的方法,例如:
public function trader_ratings()
{
return $this->morphMany(TraderRatings::class, 'rateable')->select('*', DB::raw('AVG(rating) AS avg_rating'));
}
关于php - 在字符串上调用成员函数 addEagerConstraints(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59897448/
我想计算嵌套变形关系列的平均值。 模型功能 public function trader_ratings() { return $this->morphMany(TraderRatings::
当我尝试从模型调用函数时,收到此错误 msg Call to a member function addEagerConstraints() on a non-object。这是函数: public
我尝试立即加载关系: $tournaments = Tournament::with('numCompetitors')->latest()->paginate(config('constants.P
我必须处理具有次优数据库设计的 Laravel 7 应用程序,导致标题中提到的错误。 数据库看起来像这样: mains - id - sub_type subs_a - id - main_id su
我的目标是获得对可用类(class)的平均评价。但是当我试图获得类(class)的平均评价时,它向我抛出一个错误,提示 "message": "Call to a member function ad
我一直在尝试创建一个简单的用户管理系统,但在查询关系时一直遇到障碍。例如,我有 users 和 roles,每当我尝试查询所有用户及其角色时,都会出现错误。标题中的只是我遇到的最新的一个。 我的用户和
我是一名优秀的程序员,十分优秀!