【发布时间】:2019-02-13 12:15:44
【问题描述】:
我有一个 users 和 posts 表,其中包含 user_id 和 post_views 列。 在 post_views 中,我保留了帖子显示次数的信息。
现在,在查询中,我希望 user 获得他所有帖子的 post_views 总和。
我试着做这样的事情:
User::where(['id'=>$id])->with('posts')->get();
在我定义的模型中:
public function posts()
{
return $this->hasMany('App\Models\Post')->sum('post_views','AS','totalViews');
}
但没有成功。
怎么做?
谢谢
【问题讨论】: