@armani works like a charm also with vue-toastification.
I have on the backend a slightly different usage. It is an object which can contain error, success, warning and info.
Middleware:
class HandleInertiaRequests extends Middleware
{
public function share(Request $request): array
{
return [
...parent::share($request),
'auth' => [
'user' => $request->user(),
],
'flash' => fn() => collect(['success', 'error', 'warning', 'info'])
->mapWithKeys(fn ($key) => [$key => $request->session()->pull($key)])
->filter()
->all(),
];
}
}
Toast vue component (Maybe this can be handled smarter)
<script setup lang="ts">
import { onUnmounted } from 'vue';
import { usePage, router } from '@inertiajs/vue3';
import { useToast, TYPE } from 'vue-toastification';
const toast = useToast();
const page = usePage();
let finish = router.on('finish', () => {
for (const [key, value] of Object.entries(page.props.flash)) {
if (value === null) {
continue;
}
toast(value, {
type: key as TYPE,
});
}
});
onUnmounted(() => finish());
</script>
<template></template>