mysql - How to count all comments of a post of an user in laravel elloquent model? -
my 1 many relation declaration code in user model..
public function post() { $this->hasmany('app\post', 'userid','id');// id--from user } my 1 many relation declaration code in post model..
public function comment() { $this->hasmany('app\comment', 'postid','pid');// id--from post } now want retrieve comment of user how?
try in user model
public function comments() { return $this->hasmanythrough( 'app\comment', 'app\post', 'userid', 'postid', 'id' ); } https://laravel.com/docs/5.4/eloquent-relationships#has-many-through
Comments
Post a Comment