Livewire model with relations: Cannot read properties of null

124 Views Asked by At

What's wrong? I've Laravel 10, Livewire 3.

component:

public function mount()
{
  $this->post = \App\Models\Post::with('comments')->find(1);
}

template:

<div>
  @foreach($post->comments as $index => $comment)
    <div wire:key="comment-{{ $comment->id }}">
      <input type="text" wire:model="post.comments.{{ $index }}.text">
      <pre>{{ print_r($comment) }}</pre> {{-- <- Works fine --}}
    </div>
  @endforeach
</div>

I get an js error: Uncaught TypeError: Cannot read properties of null (reading 'comments')

I have no idea. I need help.

0

There are 0 best solutions below