I’m using Inertia.js with Vue 3, and I want to show a success message when a form is submitted successfully.
In my controller I have:
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:255'
]);
Model::create($validated);
return redirect()->back()->with('success', 'Record saved successfully!');
}
In my Vue component, I tried to access the message like this:
<template>
<div v-if="flash.success" class="alert">
{{ flash.success }}
</div>
</template>
<script setup>
import { usePage } from '@inertiajs/vue3'
const { props } = usePage()
const flash = props.flash
</script>
But flash.success is always undefined even though I see the message if I dd(session('success')) in the controller.
I tried using usePage().props.flash.success, refreshing manually, and adding middleware for sessions, but nothing worked.
I expected the message "Record saved successfully!" to appear in my Vue alert after submitting the form.
How can I correctly access Laravel flash messages inside my Vue component when using Inertia?