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

David002's avatar

Laravel + Livewire updating value in database doesn't work

There is already more info in the Laravel Discord https://discord.com/channels/297040613688475649/486650445297877003/1291667060253917185

when I try to update a model with this code

    public function toggleRead(int $id): void
    {
        abort_if($this->user->role !== Role::Student, 401);

        $success = ($note = $this->getNotesBuilder()->find($id, ['read']))
            ->setAttribute('read', !$note->read)
            ->saveOrFail();

	    dd($note, $success);
    }

it doesn't save to the database (even without the dd,) even though the model shows the updated value and $success = true, I even added an updating callback on my model like this

    protected static function booting()
    {
        static::updated(function (StudentNote $studentNote) {
            dump($studentNote);
        });
    }

but this just shows the note being updated only once, so it's impossible that it gets changed and somehow immediately gets changed back, I have also checked whether the read attribute it in the $fillable array, and yes, it was. So frankly, I think this is a bug with Laravel and/or Livewire, but I have no clue why this happens.

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

You are fetching only the read column for the Note instance, so there is no ID available to save changes back to the existing record - it is null.

Solution: get the id as well as the read

$success = ($note = $this->getNotesBuilder()->find($id, ['id', 'read']))
            ->setAttribute('read', !$note->read)
            ->saveOrFail();
1 like
David002's avatar

@tykus I can't believe it, thank you for the answer. It does indeed work, I can't believe I didn't think of this, but I also thought that Laravel would somehow get the id implicitly when updating if it didn't exist yet.

Please or to participate in this conversation.