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

intossh@gmail.com's avatar

Jetstream | Inertia.js - how to 'return back()->with()'

Hi, I am using Laravel Jetstream with Inertia.js.

When I post a form using laravel-jetstream's method $inertia.form().post, this uses the $inertia.post() in the background. My controller processes the request and returns return redirect()->back().

Is there a way to send back some data? I tried return redirect()->back()->with([]); but this doesn't work.

I would like to be able to pass some data back, for example new ID of created item and have it either in the onSuccess(page) parameter or in the $page.props where jetstream's user prop is. This would be really convenient. I don't want to go through the hassle of returning new Inertia::render() instance.

Thank you

0 likes
20 replies
Sinnbeck's avatar

I personally use it like this, and it works perfectly

return redirect()->back()->with('success_message', 'Yay it worked');
mafudelaptu's avatar

dont work for me :(

how to access the "success_message" variable in Vue? do i have to setup some ServiceProvider?

Sinnbeck's avatar

You either get it from the page props (if on in the page vue file)

props: {
      success_message: String,
    },

Or using the $page helper (can be used anywhere)

<div>{{ $page.props.success_message }}</div>
spidey's avatar

I`m in the same scenario. Not able to return data using return back()->with('key', 'data'); . It only works using return Inertia::render('Homepage')->with('key', 'data'); but in this case, the route turns to mywebsite.com/submitForm which is a POST request, and if I refresh the page, an error is thrown becuase of this.

2 likes
intossh@gmail.com's avatar

Yea, I actually wasn't able to figure it out (didn't have time). To make them available, you need to pass it in the render method, which is not that useful, when you need to return back from a POST request to the original GET route, so you can't directly call the Inertia::render() method.

chilldsgn's avatar

@spidey I had the same issue and found this answer by Jonathan: "Just use a regular xhr/fetch request for this. Don't use Inertia.js."

Haven't done it yet, though, posted here in case I forget.

1 like
MaximeLeRoux's avatar

I also wanted to return back a 'status' message by doing return back()->with('status', 'data'), like it's being already done in other Laravel controllers (Fortify), but I couldn't get the result in my Vue app other than by re-rendering the view with Inertia::render() directly in the called route method.

In order to get this data you can either declare a shared key ('status') in the App\Http\Middleware\HandleInertiaRequests middleware, which will always be sent in each Inertia request. The 'data' is stored in the server-side user session:

    /**
     * Defines the props that are shared by default.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function share(Request $request)
    {
        return array_merge(parent::share($request), [
            'status' => session('status'),
        ]);
    }

Or, if you need to send specific data back only in one route, you should add a prop in the route method which generates the view (since back() will re-run the view rendering method), and also declare it in your Vue component (User/Show) by doing props: ['user', 'status']:

    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function __invoke(Request $request)
    {
        $user = $request->user(); // Some logic here

        return Inertia::render('User/Show', [
            'user' => $user,
            'status' => session('status') // Add this
        ]);
    }

Hope it helps!

1 like
Stingoosha's avatar

Try to configure 'flash' in the App\Http\Middleware\HandleInertiaRequests middleware


    /**
     * Defines the props that are shared by default.
     *
     * @see https://inertiajs.com/shared-data
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function share(Request $request)
    {
        return array_merge(parent::share($request), [
            'flash' => function () use ($request) {
                return [
                    'success' => $request->session()->get('success'),
                ];
            },
        ]);
    }

Now in controller use such construction


return redirect()->back()->with('success', 'some message');

And in view get this message


{{ $page.props.flash.success }}

Hope, You will succeed it!

11 likes
Mmaark's avatar

You can pass the session as prop manually in the controller, since redirect is making the browser do an additional GET request to your route, you will have access to the session:

Route::get('/where-the-back()-pointing-to', function (Request $request) {
    return Inertia::render('YourPage', ['status' => $request->session()->get('status')]);
});
abdosaeedelhassan's avatar

In your shared data, you need to get it as follow $page->props->varName by the way in console the varible shown null, i think this is bug, but the value passed successfully and you can access as above code statement

spectral_flux's avatar

The best way I found was to do as such: In the controller

		   if ($result) {
            session()->flash('flash.type', 'success');
            session()->flash('flash.message', 'Dados atualizados!');
        } else {
            session()->flash('flash.type', 'error');
            session()->flash('flash.message', 'Erro ao atualizar!');
        }
        return Redirect::back();

And in my vue component:

...
 onSuccess: (data) => {
                    this.toast(this.$attrs.jetstream.flash.type, this.$attrs.jetstream.flash.message)
                },
....
hamid_imomov's avatar

Controller:

return redirect()->back()->withErrors(['db_error'=>'any message']);

Vue template:

<form @submit.prevent="submit">
...
<div v-if="form.errors.db_error">
{{ form.errors.db_error }}
</div>
</form>
blaubit's avatar

I know I am reviving this again, but I wonder why the most simply solution is not present here. There is no global way, beside modifying the inertia handler, or without using the session controller.

Just define the session getter in the initial render, and you will then be able to read it:

return Inertia::render('Payment/Payment', [ 'payment_url' => session('payment_url'), ]);

And later: return back()->with('payment_url', $paymentUrl);

1 like
jox51's avatar

Not sure why Inertia doesn't mention this but in your Controller when you return like so:

  return redirect()->back()->with('status', 'success');

This will return back the status as a query params

http://127.0.0.1:8000/app?status=success 

In your component, I am using React, you can grab this like so using Inertia's usePage hook:

   const { url } = usePage();
   const status = new URLSearchParams(url.split("?")[1]).get("status");

Hope this helps someone.

catalin12's avatar

For me, the best solution was this workaround:

        $previousRouteName = Route::getRoutes()->match(
            Request::create(URL::previous())
        )->getName();

		return to_route($previousRouteName, [ 'status' => 'success'];

And now you can get them with $request->all() . I find this more elegant as you do not need to modify the session or anything else It look like this is a bug in inertia itself

Please or to participate in this conversation.