M
M
Mo1she2019-02-18 10:48:07
Laravel
Mo1she, 2019-02-18 10:48:07

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');
    }

and in Comment.php -
public function commentable()
    {
        return $this->morphTo();
    }

Ultimately, in order to get news comments, I do this -
$comments = $news->comments();
And here's the actual question: how can I get the post to which this comment belongs to by a certain comment? How to implement something like this -
$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 question

Ask a Question

731 491 924 answers to any question