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

thetanaz's avatar

Why are error's not sent to the front end by default in laravel+inertia+react?

So I have a form in my project, that if successfully submitted redirects to the home page, as it is a report form so the user reports a problem and then they're redirected home. The issue I ran into is that I wanted to make a simple success toast in my react front end so that the user knows that their submission has succeeded but I realized there's no built-in way to handle errors from flash data from the session. So this is the back-end method for reference:

public function store(ListingReportRequest $request)
    {
        try {
            $data = $request->validated();
            $data['reporter_id'] = $request->user()->id;
            $report = ListingReport::create($data);
            return redirect()->route('home')->with('success', 'Report submitted successfully..');
        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'Something went wrong when submitting the report.');
        }
    }

Pretty simple.

But in order for me to use that data I had to go into the handleinertiarequests middleware and add it to the response

                'error' => fn() => $request->session()->get('error'),
                'success' => fn() => $request->session()->get('success')
            ],

and then on my front end I could consume it and display it as a toast

  const { flash } = usePage<PageProps>().props;

    useEffect(() => {
        if (flash && flash.error) {
            toast.error(flash.error);
        }
        if (flash && flash.success) {
            toast.success(flash.success);
        }
    }, [flash]);

My question is, is there a better way to send error messages back to my react front-end that is built in? Now this is not a problem if I'm staying on the page I'm submitting the form in, because I can use axios to save the response and display it, but if I'm redirecting I don't know of a built-in way to do this.

Sorry if the explanation is a bit lackluster, feel free to ask for clarification if you don't get my question.

and then consume it on the front end

0 likes
2 replies
jaseofspades88's avatar

Why would sending errors back to the view be any different than say passing explicit data back from an API resource?

Please or to participate in this conversation.