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

elefant793's avatar

Eloquent Model in wire:model => Uncaught TypeError: Cannot read properties of null

Hi, when I'm trying to use my Eloquent Model for the data binding with wire:model I'm getting an error in the console Uncaught TypeError: Cannot read properties of null (headline) My Livewire Component

<?php

namespace App\Livewire\Events;

use App\Models\Event;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Livewire\Component;

class EventView extends Component
{
    public $id;
    public array|null|Builder|Collection|Model $event;
    public string $currentTab;
    public bool $editing;

    public string $textarea;

    public function mount($id)
    {
        $this->id = $id;
        $this->event = Event::with(['user', 'invitees.user'])->findOrFail($this->id);
        $this->currentTab = 'home';
        $this->editing = true;
        $this->headline= $this->event->headline;
    }

    public function render()
    {
        return view('livewire.pages.event.event-view')
            ->layout('layouts.app')
        ;
    }

    public function gotoTab(string $tab = 'home'): void
    {
        $this->currentTab = $tab;
    }

    public function setEditing(): void
    {
        $this->editing = true;
    }

    public function updateEvent(): void
    {
        dd($this->event->text);
    }
}

And my blade file (throws the error)

@if($editing)
<x-text-input wire:model="event.headline" class="block mt-1 w-full" type="text"  />
@else
{{ $event->headline }}
@endif	

But when I'm using the $headline which i also set in the mount() it works

Anyone got any ideas? Thanks

0 likes
2 replies
vincent15000's avatar

Have you tried this ?

@if ($event)
	@if ($editing)
		<x-text-input wire:model="event.headline" class="block mt-1 w-full" type="text"  />
	@else
		{{ $event->headline }}
	@endif
@endif	
elefant793's avatar

@vincent15000 still the same Error. The weird thing is, when $editing is false, the else part works perfectly fine and prints the headline

Please or to participate in this conversation.