2017-04-07 80 views
0

我一个在用户模型许多关于申报代码..如何计算用户在laravel elloquent模型中发表的所有评论?

public function post() 
{ 
    $this->hasMany('App\Post', 'userid','id');// id--from user 
} 

我的一个在Post模型许多关于申报代码..

public function comment() 
{ 
    $this->hasMany('App\Comment', 'postid','pid');// id--from post 
} 

现在我想检索用户的所有评论但是如何?

回答

0

的意见是不相关的用户那里,你可以查询是我猜你应该“在用户的帖子所有评论”

0

改变你的模型方法并将其复数化:

用户型号:

public function posts() 
{ 
    $this->hasMany('App\Post', 'userid','id');// id--from user 
} 

Post模型:

public function comments() 
{ 
    $this->hasMany('App\Comment', 'postid','pid');// id--from post 
} 

在这之后,你可以做到这一点在你的控制器来计算用户的评论:

$user->posts->comments->count(); 
相关问题