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

maogu's avatar
Level 1

Why Toast aways show null ?

I use laravel Inertia Js Latest version ,

// web.php
Route::post('/test', function () {
    
//    session()->flash('toast', 'Test Toast');
//    dd(session('toast')); //Is show 'Test Toast'
    
   return redirect()->back()->with('toast', 'Test Toast');
});

//HandleInertiaRequests.php

public function share(Request $request): array
    {
        return array_merge(parent::share($request), [
            'toast' => $request->session()->get('toast'),
        ]);
    }

//vue
const  form = useForm({});

function  submit() {
    form.post('/test')
}

    <form @submit.prevent="submit" class="flex flex-col">

        <button >Test</button>
    </form>

0 likes
4 replies
LaryAI's avatar
Level 58

It looks like you are trying to retrieve the 'toast' value from the session, but it is not being set properly in the first place.

Make sure that the 'toast' value is being set correctly in the session when you redirect back. You can check this by adding a dd(session('toast')) after the redirect in your controller.

If the value is not being set, try using the with() method like this:

return redirect()->back()->with(['toast' => 'Test Toast']);

If the value is being set correctly, make sure that you are retrieving it correctly in your view. You can try using the session() helper function directly in your view like this:

@if(session('toast'))
    <div class="toast">{{ session('toast') }}</div>
@endif

Also, make sure that you are passing the 'toast' value to your view correctly. You can try using the with() method like this:

return Inertia::render('YourView')->with('toast', session('toast'));
1 like
CamKem's avatar

redirect back doesn't work using the with() help when you use Inertia. A solution to this is to flash the toast to the session (like you have commented out in your code block) & then track it with the middleware (also like you have in your code block), then in Vue, you can use $page.prop.toast in your < template > & usePage().prop.toast in your < script > tags.

maogu's avatar
Level 1

@CamKem thank you for your reply. I found the problem. It should be version 1.0 of Inertia. I also feel that Inertia is not very active. I read your other post about choosing Splade or Inertia. I have written two projects with livewire before, and I am currently working on them. Write your first Inertia project. If it is splash and livewire, I will choose livewire, splash+blade. If you create a new user on the user list page, you must write post routing, livewire does not need to write post routing

CamKem's avatar

@maogu I know what you mean with Inertia not being active, I made an Issue on the github repo about a week ago & no one from the core team replied (only other people with the same problem), then I looked at all the open issues - it's quite concerning as the community is strongly pushing Inertia.

Hopefully @TaylorOtwell can add Splade as an option for Breeze & Jetstream install with laravel in the next release.

Please or to participate in this conversation.