vStubbs
vStubbs

Reputation: 41

Laravel Inertia.js, flash messages dissapearing

I'm trying to implement flash messages upon a redirect. I added

        'flash' => [
            'message' => fn () => $request->session()->get('message')
        ],

to the HandleInertiaRequests.php, and I have a simple Inertia link posting to a controller that redirects back as so

return \redirect()->back()->with('message', 'Item added successfully!');

On the front end there's just

<div v-if="$page.props.flash.message">{{$page.props.flash.message}}</div>

The problem is that the message appears for half a second or so, and then disappears. It's as though the page gets refreshed as soon as it changes.

Any ideas what might be causing this?

EDIT: turned out that the issue was with the dependency declaration in my package.json. I had

"@inertiajs/inertia": "^0.11.0",

whereas I got it working after npm install with

"@inertiajs/vue3": "^2.0.0",

Near as I can tell, the core issue this produced was that the $page object itself was undefined as usePage() wouldn't work despite its import being present.

Upvotes: 0

Views: 75

Answers (1)

Jakeb S K
Jakeb S K

Reputation: 11

Have you tried:

session()->flash('success', 'Your action was successful!');

This is the correct way to persist flash message, according to this forum, and is the method I personally use when flashing messages.

Upvotes: 0

Related Questions