在字符串上调用成员函数addEagerConstraints()

Zain Farooq

我想计算嵌套变形关系列的平均值。



型号功能

public function trader_ratings()
{
    return $this->morphMany(TraderRatings::class, 'rateable')
        ->select('rateable_id', 'rateable_type', 'rating')
        ->avg('rating');
}

延迟加载的控制器

$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'));
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在laravel中的字符串上调用成员函数format()

错误:在字符串上调用成员函数setPayrollperiodid()

在字符串上调用成员函数all()(查看:

Laravel错误:在字符串上调用成员函数format()

PHP:在字符串上调用成员函数contains()

致命错误:在字符串上调用成员函数prepare()

Yii 2 在字符串上调用成员函数 saveAs()

错误:在字符串上调用成员函数 addDays()

致命错误:在字符串上调用成员函数

在字符串上调用成员函数fill()

在字符串上调用成员函数 setDate()

在 Yii 的字符串上调用成员函数 saveAs()

在laravel中的字符串上调用成员函数latest()

在字符串上调用成员函数guessExtension()

Laravel 5.6:在字符串上调用成员函数getRealPath()

错误:在字符串上调用成员函数storeAs()

在字符串上调用成员函数getRealPath()

在 Laravel 的字符串上调用成员函数 has()

在字符串上调用成员函数canBeViewed()

Laravel - 在字符串上调用成员函数 save()

在字符串上调用成员函数notify()

致命错误:在字符串上调用成员函数Createuser

在字符串上调用成员函数getRepository()

在字符串上调用成员函数 move()

Laravel 错误:在字符串上调用成员函数 format()

Laravel:在字符串上调用成员函数 with()

在整数上调用成员函数addEagerConstraints()

在浮动LARAVEL上调用成员函数addEagerConstraints()

调用成员函数where()在字符串上