我试图通过关系获得平均值
我正在做的是:$user_detail=Users::withCount('followers')->withCount('followings')->withCount('posts')->with('ratings')->with('posts')->where('user_id',$user_id)->get();
public function ratings()
{
return $this->hasMany('\App\Reviews','review_user_id','user_id')->selectRaw('review_user_id, avg(rate) AS avg')->groupBy('review_user_id');
}
现在这是有效的,我得到的是:
"followers_count": 0,
"followings_count": 1,
"posts_count": 1,
"ratings": [
{
"review_user_id": "2c61d7a0-7d05-11e7-8dab-6d6ba8a72982",
"avg": 3.25
}
],
我想要的是:
"ratings": 3.25
任何帮助都将不胜感激。
PS:Laravel 5.2
答案 0 :(得分:0)
这应该这样做
return $this->hasMany('\App\Reviews','review_user_id','user_id')->
selectRaw('review_user_id, avg(rate) AS avg')->
groupBy('review_user_id')
->pluck('avg');