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

Russelmhardy's avatar

Handling Errors and Success Responses with Inertia.js and Laravel

Hello everyone,

I'm currently working on a project using Inertia.js and Laravel, and I'm facing some difficulties in handling errors and success responses properly. I've followed the documentation and tried different approaches, but I couldn't get it to work as expected.

The issue I'm facing is that the onError and onSuccess callbacks in my Inertia.js request are not working as intended. Only the onBefore callback is functioning properly. Whenever an error occurs, I receive a Laravel error page with a "500 Internal Server Error" message instead of being able to handle the error within my Vue.js component.

Here's a simplified version of the code I'm using in my Vue.js component:

javascript router.post(route("posts.update", this.tweet.id), formData, { onBefore: () => confirm("Are you sure you want to delete this user?"), onError: () => { return Promise.all([confirm("Are you sure you want to delete this user?")]); }, onSuccess: () => { return Promise.all([confirm("Are you sure you want to delete this user?")]); }, });

My Laravel Code :

try {

        $newPost = json_decode($request->post, true);
        $post->fill(json_decode($request->post, true));
        $idsToDelete = array_column($newPost['files'], 'id');
        Media::whereIn('id', $idsToDelete)->delete();
        foreach ($request->files as $key => $value) {
            $post->addMedia($value)->toMediaCollection('files');
        }
        
        $post->save();
        return response()->json(['good' => "With Sucess"], 200);
    } catch (Exception $e) {
        return response()->json(['error' => $e->getMessage()], 500);
    }

I have also configured my Laravel backend to return JSON responses for API requests. However, the error handling is not working as expected, and I'm unable to display custom error messages or perform specific actions when an error occurs.

I would greatly appreciate any guidance or suggestions on how to properly handle errors and success responses in Inertia.js and Laravel. If you have any sample code, tips, or alternative approaches to share, it would be incredibly helpful.

Thank you in advance for your assistance!

Feel free to adjust the text as per your specific requirements and include any additional details or code snippets that might help forum members understand your issue better.

0 likes
1 reply

Please or to participate in this conversation.