我有一个表的用户和帖子的列USER_ID和post_views。在post_views中,我保留了显示多少次帖子的信息。
现在,在查询中,我想让用户获得其所有帖子的post_views之和。
我试图做这样的事情:
User::where(['id'=>$id])->with('posts')->get();
Run Code Online (Sandbox Code Playgroud)
在模型中,我定义了:
public function posts()
{
return $this->hasMany('App\Models\Post')->sum('post_views','AS','totalViews');
}
Run Code Online (Sandbox Code Playgroud)
但是没有成功。
怎么做?
谢谢