【发布时间】:2019-08-09 11:37:23
【问题描述】:
我在 Laravel 中有三个模型。
UserProfileRating
User 有很多 Profiles,Profile 有很多 Ratings。
我的目标是获取用户拥有的评分总数。像这样的:
$user->profiles()->ratings()->count();
我怎样才能得到这个总数?
任何帮助将不胜感激,在此先感谢!
【问题讨论】:
我在 Laravel 中有三个模型。
UserProfileRatingUser 有很多 Profiles,Profile 有很多 Ratings。
我的目标是获取用户拥有的评分总数。像这样的:
$user->profiles()->ratings()->count();
我怎样才能得到这个总数?
任何帮助将不胜感激,在此先感谢!
【问题讨论】:
您必须在用户模型中添加功能:
public function profile()
{
return $this->hasMany('App\Models\Profile', 'user_id', 'id');
}
并在配置文件模型中添加功能:
public function rating()
{
return $this->hasMany('App\Models\Rating', 'profile_id', 'id');
}
然后像这样调用:
User::withcount('profile.rating');
【讨论】:
【讨论】: