Answer the question
In order to leave comments, you need to log in
Laravel polymorphic relationship?
Hello. I have news and articles on my site. And you can leave comments on news and articles. I am using polymorphic relationship to link these entities.
In order to implement this, I placed in News.php and Article.php -
public function comments()
{
return $this->morphMany('App\Entity\Comment\Comment', 'commentable');
}
public function commentable()
{
return $this->morphTo();
}
$comments = $news->comments();
$post = $comment->post();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question