Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

croftCoder's avatar

Undefined variable though variable is defined

class TestItem extends Component
{
    public $item;

    public function mount($item)
    {
        $this->item = $item;
    }

    public function render()
    {
        return view('livewire.test-item', ['item' => $this->item]);
    }
}
<!-- test-item.blade.php -->
<div>
    {{ $item }}
</div>
class TestComponent extends Component
{
    public function render()
    {
        return view('livewire.test-component');
    }
}
<!-- text-component.blade.php -->
@foreach($cart->cart_items as $item)
<livewire:test-item :item="$item" :key="$item->id" />
@endforeach

getting the error: undefined variable $item in the test-item.blade.php

0 likes
3 replies
tykus's avatar

If $item is a public property on the Component class, then you should not also pass item as data to the view template:

class TestItem extends Component
{
    public $item;

    public function mount($item)
    {
        $this->item = $item;
    }

    public function render()
    {
        return view('livewire.test-item');
    }
}

Please or to participate in this conversation.