2017-04-07 79 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(); 
相關問題