samedi 30 mars 2019

How can I loop in polymorphic relation by passing query reference?

I dont know if its called query reference. Feel free to make edits.

I have a polymorphic relation on my Article Model:

public function comments(){

        return $this->morphMany(Comment::class, 'commentable');

    }

Now I have to send a polymorphic relation data to my view.

public function show(Article $article)
    {

        return view('article.show',[
            'article' => $article,
            'comments' => $article->comments()
        ]);
    }

I'm sending it by $article->comments() because I want to use each comments eloquent relations.

On my blade:

@foreach($comments as $comment)
                 //This won't execute. What is wrong?
@endforeach

if I dd(comments) before foreach I've this: enter image description here



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire