[UserReviews
类后续代码。
public function getReviewAvgAndCount() {
return $this->hasMany(UserReview::class, 'tractor_id');
}
function getAverageRatingAttribute() {
return ($this->getReviewAvgAndCount()->count());
}
UserReview:
public function tractorTable()
{
return $this ->belongsTo('App\Model\TractorTable','tractor_id');
}
呼叫控制器:
TractorTable::with('tractorImage','tractorSpecData','getAverageRatingAttribute','drive')->orderBy('tractor_id', 'desc')->take(5)->get();
急切加载(带有)只能用于关系:https://laravel.com/docs/6.x/eloquent-relationships#eager-loading
所以getAverageRatingAttribute
无效。