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

sajadsholi's avatar

handle laravel exception in livewire

My backend logic exists in vendor, so I do not access the logic, and in some cases, it throws exceptions

For front, I use Livewire when an exception tis hrown, the Livewire shows 404, 403 , 401 ... blade.php files

I did something like this in bootstrap/app.php

    ->withExceptions(function (Exceptions $exceptions): void {
        $exceptions->render(function (Exception $e, Request $request) {

            if ($request->is('livewire/update')) {

                if (
                    $e instanceof AuthorizationException ||
                    $e instanceof AccessDeniedHttpException
                ) {
                    return redirect()->back()->with('error-toast', $e->getMessage());
                }
            }
        });
    })

But since the Ajax request from Livewire gets 302 and is redirected to the back URL the flash data will be destroyed before using

And by the way, I do not like this approach. I want to tell Livewire an exception happened do something about it. what should I do?

0 likes
2 replies
jlrdw's avatar

Can't you just show the exception, and have a button on the same page to link to (whatever page).

martinbean's avatar

@sajadsholi Then actually catch the exceptions in your Livewire component so you can update your component’s state appropriately?

Please or to participate in this conversation.