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

CamKem's avatar
Level 10

Inertia - Redirect to route not passing object in with()

Hello, I am working along with the Inertia SPA techniques series & am trying to implement flash message into my controllers. I have the Vue & Inertia front end side of this working perfectly, however when I try passing my flash message using ->with() as part of a route redirect, the object is not passed to the client. I checked in inertia js devtool & there the "flash" array is not present. This is the method I a referring to in my UsersController.php

    public function update(User $user)
    {
        $attributes = Request::validate([
            'name' => 'required',
            'email' => ['required', 'email'],
        ]);

        $user->update($attributes);

        return redirect()->route('users.show', $user->id)
            ->with('flash', ['success' => 'User updated successfully']);
    }

it must have something to do with the redirect()->route or redirect()->back() method when using inertia. I know this because when I add a with() to return Inertia::render it works fine & passed the data, such as in this method here:

    public function show(User $user)
    {
        $user = $user->only(['name', 'email']);
        return Inertia::render('Users/Show', [
            'user' => $user,
        ])->with('flash', ['success' => 'User loaded successfully']);

    }

So I guess the question is, how do I get redirects to include objects in the request when using Inertia?

As always, I appreciate your assistance!

EDIT: I tried the withViewData() helper does not exist on Inertia::location() - as per this die & dump share: (https://flareapp.io/share/4m4xQa4m#F51) surely this is an issue that many people have faced? I want to return the object as part of the inertia response if possible. Can anyone assist?

0 likes
1 reply
CamKem's avatar
CamKem
OP
Best Answer
Level 10

I created a work around - I added to HandleInertiaRequests.php middleware the following:

            'flash' => [
                'success' => fn () => $request->session()->get('success'),
                'error' => fn () => $request->session()->get('error'),
                'warning' => fn () => $request->session()->get('warning'),
                'info' => fn () => $request->session()->get('info'),
            ],

Then in my controller just flash() the message to the session, so it is picked up by the middleware.

    public function update(User $user)
    {
        $attributes = Request::validate([
            'name' => 'required',
            'email' => ['required', 'email', 'unique:users,email,' . $user->id],
        ]);

        $user->update($attributes);

        session()->flash('success', 'User updated successfully');

        return redirect()->route('users.show', $user->id);
            // These don't work... try to find a fix?
            //->withData(['flash' => ['success' => 'User updated successfully']]);
            //->with(['flash' => ['success' => 'User created successfully!']]);
    }

Please let me know if there is a simpler way, without adding to the inertia middleware.

Please or to participate in this conversation.