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

Rafaelindriago's avatar

"ModelNotFound" exception handler for Livewire

Hello guys, when i put a Model as public property on a Livewire component and load the model from database, if the model is deleted from another tab ot another browser, any interaction with the component thrown the "NOT FOUND" modal, on the docs explains how to handle the modal with Javascript, but the component still throwing the "NOT FOUND" message, the only solution i found was to force the entire page reload, but i wonder, is there anyway to handle this behaviour on PHP? Something like the "model missing" behaviuor on normal routes with controllers.

use App\Http\Controllers\LocationsController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
 
Route::get('/locations/{location:slug}', [LocationsController::class, 'show'])
        ->name('locations.view')
        ->missing(function (Request $request) {
            return Redirect::route('locations.index');
        });
0 likes
7 replies
webrobert's avatar

the route, model binds and should throw a 404 not found. It seems like the issue is probably elsewhere.

LocationsController::class is a normal controller, then you are trying to pass a model to a Livewire component?

Rafaelindriago's avatar

@webrobert is not an issue, is just if a model is on a public property of a component, if you delete that model from another side, like other tab or directly on database, then when interact again with the component, it always will throw 404, i want to know is there anyway to handle this, on my case, i prevent the modal from Livewire and reloading the entire page

Rafaelindriago's avatar
<?php

namespace App\Http\Livewire;

use App\Models\Post;
use Livewire\Component;

class PostEdit extends Component
{
    public Post $post;

    protected $listeners = [
        'edit-modal:open' => 'open',
    ];

    protected $rules = [
        'post.title'    => ['required', 'string', 'max:255'],
        'post.content'  => ['required', 'string', 'max:2048'],
    ];

    public function mount()
    {
        $this->post = Post::make();
    }

    public function render()
    {
        return view('livewire.post-edit');
    }

    public function open($key)
    {
        $this->post = Post::findOrFail($key);
    }

    public function save()
    {
        $this->validate();

        $this->post->save();

        session()->flash('stored', true);

        $this->resetValidation();

        $this->emit('table:refresh');
    }

    public function close()
    {
        $this->post = Post::make();

        $this->resetValidation();
    }
}

That is the code of a component to edit a Post model.

<script>
            window.addEventListener('livewire:load', () => {
                Livewire.onError((message, statusCode) => {
                    window.alert('Something went wrong!')

                    console.log(message, statusCode)

                    return false;
                })
            })
</script>

And that is for prevent Livewire modal, like it says in the docs.

If i open the modal it loads the model and put it at the public property "post", i can binding directly to properties on that model and everything ok, but if i delete the model in another side, like another tab, the component return always 404 for any interaction, this is not an issue, is the default behaviur i guess, for that i want to know is there anyway to handle this, somethins like "missing model" on routes or something.

Rafaelindriago's avatar
Rafaelindriago
OP
Best Answer
Level 1

I guess this because when Livewire tries to "hydrate" the component never will found a deleted post, but the Javascript side of Livewire still sending that model ID that no longer exists... I found two "solutions": reload the entire page with Javascript or avoid to use Models as properties, use an array instead and load the model just for fill the array and then fill the model back and save to database, in that way i can handle the "findOrFail" method exception and force the modal to close with "dispatchBrowserEvent", but if you know something better to handle this.

Snapey's avatar

@Rafaelindriago how about only storing the id and then using a computed property to load the model (or handle missing model)

https://livewire.laravel.com/docs/computed-properties

If you are wire:model to the model as a public property, you may need to copy attributes of the model to an array.

Remember to make the ID readonly

If another user can cause issues by deleting the model, you might also think about the effects of them just editing the model values.

webrobert's avatar

well the simplest answer is dont use livewire for tabs. We're aren't to livewire 3 just yet. Otherwise fire an event from where ever you create the issue deleting the model and add a listener on the problem component that refreshes the component so it loads a new instance from your mount method.

1 like

Please or to participate in this conversation.