Polymorphic relationship

Posted 1 year ago by chezzemp

I have the following tables: Users, Videos, Post, Comments. (It is with the traditional relationship, Commentable, return $this->morphTo(), etc... The fault is in the consultation)

I have to make a polymorphic relationship, what happens is that users can only make one comment, the question is ... How to access that comment without using foreach?

It is currently like this.

@foreach($posts as $item)
    {{ $item->title }}
    //This is the problem, it is only a single comment, I would not like to use foreach
    @foreach($item->comments as $comment)
        {{ $comment->body }}

Many thanks friends,

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up


Reply to

Use Markdown with GitHub-flavored code blocks.