Undefined variable inside foreach

@foreach($contents as $content)
        @livewire('front-end.social.timeline', ['content' => $content], key($loop->index))
   @endforeach

Could someone please tell me why this is giving me an error? This is how the manual indicates that we have to pass the variables to a child component.

the $contents collection is defined because if I do this, it works:

@foreach($contents as $content)
        {{$content}}
@endforeach

The error only comes when passing it to the livewire component.

maybe do like

@livewire('front-end.social.timeline', ['content' => $content], key($content->id))

or

@foreach($contents as $index => $content)
        @livewire('front-end.social.timeline', ['content' => $content], key($index))
@endforeach

???

same error:

Undefined variable: content (View: C:\laragon\www\doogmas\resources\views\livewire\front-end\social\timeline.blade.php)

I’m sorry, it’s a stupid mistake, I didn’t declare the variable in the child component. I’ve been programming for too many hours…

1 Like