inertiajs / inertia-laravel

The Laravel adapter for Inertia.js.
https://inertiajs.com
MIT License
2.03k stars 227 forks source link

NOT WORKING: with() helper on "return redirect->route()" #514

Closed CamKem closed 1 year ago

CamKem commented 1 year ago

Hello, I am trying to pass data using the with() helper during a redirect->back() or redirect->route() return in my Laravel controller & have found that inertia is not passing the data when the user is redirected by inertia. I solved this by using something like the following:

// In HandleInertiaRequests.php middleware, add this object to the array of shared data
            '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, when I want to use a redirect(), use the following line inside the method logic, to store the data in. the session so that inertia can retrieve it & pass it to the client.

        session()->flash('info', 'User created successfully');

While this way of solving the problem works, it's annoying to have to manually recreate the objects in the middleware when using redirect(), I would think that as with() work fine if you are doing an Inertia::render() that the maintainers / creators of Inertia can add this feature in so if we are doing a redirect() & it has a with() second argument, that inertia automatically passes the data without registering an object in middleware.

Maybe there is a solution to this, I would appreciate any ideas!

beiaduo commented 1 year ago

me too

CamKem commented 1 year ago

it seem Inertia now uses:

return to_route('route.name')

However this still does not accept ->with() . I mean it doesn't throw an error, but it also doesn't pass it to the client. Flash the session is all we can do for now.

Also, I might mention, it seems Inertia is not being maintained now? No one is answering the Issues...

RobertBoes commented 1 year ago

The with() in this case wouldn't have much to do with Inertia, that's a Laravel thing. When returning a redirect()->with() the data passed to with would be flashed to the session, see https://laravel.com/docs/10.x/responses#redirecting-with-flashed-session-data

Inertia also provides an example for this in the docs: https://inertiajs.com/shared-data#flash-messages

While this way of solving the problem works, it's annoying to have to manually recreate the objects in the middleware when using redirect(), I would think that as with() work fine if you are doing an Inertia::render() that the maintainers / creators of Inertia can add this feature in so if we are doing a redirect() & it has a with() second argument, that inertia automatically passes the data without registering an object in middleware.

Thing is here, there's no way to tell where this data originates from. And if Inertia would just share everything that's present in the session it would be a huge security risk.

CamKem commented 1 year ago

We are aware of how to flash data to the session & then read it in the Inertia middleware.

The issue is related to Inertia. Seeing as Inertia is shipped as an install option for the Laravel Starter Kits, I would think that the teams at Laravel & Inertia would work together to resolve this issue, so that on a return that uses the with() function is supported with Inertia.

You say it's a Laravel issue, but it works fine with Blade components & other front end implementation, the only time that there is an issue with using it (that I have come across so far, is when I use Inertia). So as I said, it would be great to be able to use with() with redirecting to a route using inertia (this includes back(), to_route(), route(), etc...)

aifodu commented 1 year ago

My problem was that I forgot to add \App\Http\Middleware\HandleInertiaRequests::class to the web middleware group. Once I did everything worked as normal.

Using:

beiaduo commented 1 year ago

When you use that same time doesn't work ,now I remove about that

jessarcher commented 1 year ago

Hey there,

We're closing this issue because it's inactive, already solved, old or not relevant anymore. Feel to open up a new issue if you're still experiencing this problem.