Search code examples
phplaraveldockersessioninertiajs

Laravel flash messages not working as expected


I'm confused right now. I have a Laravel/Inertia/Vue app I'm trying to Dockerize. However, I have an issue with the flashed session I'm using. I have a controller with the following method:

public function testMethod()
    {
        session()->put("success", "message1");
        return back()->with("testMessage", "message2");
    }

When my component in Vue is mounted, I call the endpoint, which in turn calls this function using the $inertia adapter.

mounted() {
        this.$inertia.post("/testEndpoint", {});
}

I am using the HandleInertiaRequests.php Middleware as suggested in the inertia JS documentation. I defined the share method in the following way:

public function share(Request $request): array
    {
        return array_merge(parent::share($request), [
            "flash" => function () {
                return [
                    "testMessage" => session()->get("testMessage"),
                    "success" => session()->get("success"),
                    "error" => session()->get("error"),
                    "status" => session()->get("status"),
                    "data" => session()->get("data")
                ];
            },
        ]);
    }

When I refresh the page and call the endpoint I get the following object in response:

{
    "component": "Landing",
    "props": {
        "flash": {
            "testMessage": null,
            "success": "message1",
            "error": null,
            "status": null,
            "data": null
        },
    },
    "url": "\/",
    "version": "0f37ca5b6d996b9ca5092f1730b72791"
}

That is, the "success" session key is working as expected but not the "testMessage" which is created using with().

I am using Laravel 10.39 and inertia-laravel 0.6.9.

Thanks for the help!

The really weird thing is that this used to work up until last week, and I don't think I made changes that could have broken the app since then. I even tried to revert to a previous Git and rebuild the app, but now I have the same issue, which I didn't have previously.

I tried to clear the cache of the Laravel app and didn't change the content/order of my Middlewares.

This bug appeared after I rebuilt my containers, which I hadn't done in a while. I played a bit with the permissions for my container files last week. Could this be why?


Solution

  • After days of investigation, I finally figured out what went wrong! This is a bug with the Laravel Debugbar I had installed. Version 3.13.0 came out recently, causing this behavior; if I revert to 3.12.4, it works fine. I'll submit an issue on the package's GitHub repository.