inertiajs / inertia-laravel

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

Can't send data back from a redirect #269

Closed thecyrilcril closed 3 years ago

thecyrilcril commented 3 years ago

When Submitting a form from a child component (non-page component) using this syntax in the Composition API

    const form = useForm({
      first_name: null,
      email: null,
      mobile_number: null,
    })

form.submit('post', route('leads.store'), {
   preserveScroll: true,
   onSuccess: () => form.reset(),
})

I can't pass data back either to the Parent(Paged) Component or the Component sending the request itself

I have tried the redirect syntax return Redirect::route('...')->with('key', 'value'); but I could not see my key on the Parent Components which is the Page component loaded by the route.

I understand I can't use the Inertia::render('...', [])syntax as it messes with the routing.

How do I get data back to the view from the redirect?

Laravel Project with Jetstream/Inertia Laravel Framework 8.33.1

λ npm list --depth=0 C:\wamp64\www\kehindeoni +-- @babel/plugin-syntax-dynamic-import@7.8.3 +-- @fontsource/commissioner@4.2.2 +-- @fontsource/spectral-sc@4.2.2 +-- @inertiajs/inertia@0.8.5 +-- @inertiajs/inertia-vue3@0.3.5 +-- @inertiajs/progress@0.2.4 +-- @tailwindcss/forms@0.2.1 +-- @tailwindcss/typography@0.3.1 +-- @vue/compiler-sfc@3.0.7 +-- autoprefixer@10.2.5 +-- axios@0.21.1 +-- browser-sync@2.26.14 +-- browser-sync-webpack-plugin@2.2.2 +-- eslint@7.22.0 +-- eslint-plugin-vue@7.8.0 +-- laravel-mix@6.0.13 +-- lodash@4.17.21 +-- postcss@8.2.15 +-- postcss-import@12.0.1 +-- tailwindcss@2.1.2 +-- video.js@7.11.8 +-- videojs-hotkeys@0.2.27 +-- videojs-youtube@2.6.1 +-- vue@3.0.7 +-- vue-loader@16.1.2

Lloydinator commented 3 years ago

Redirect and its methods are part of Laravel core, not Inertia. Plus, that data is flashed to the session, so you'll have to share the data with Inertia in AppServiceProvider. Seems to me that would be your issue. So you would do it like this:

    public function boot()
    {
        Inertia::share('flash', function(){
            return [
                'message' => Session::get('message')
            ];
        });
    }

If you do a console.log in your frontend you should see it under flash.

thecyrilcril commented 3 years ago

Thanks @Lloydinator

Redirect and its methods are part of Laravel core, not Inertia. Plus, that data is flashed to the session, so you'll have to share the data with Inertia in AppServiceProvider. Seems to me that would be your issue. So you would do it like this:

    public function boot()
    {
        Inertia::share('flash', function(){
            return [
                'message' => Session::get('message')
            ];
        });
    }

If you do a console.log in your frontend you should see it under flash.