If you find PHPFlasher useful, please consider giving it a ⭐ star on GitHub 😊. If you spot any typos or have suggestions, feel free to contribute to the documentation 📝. Your feedback helps keep the project up-to-date and well-maintained. Thank you
PHPFlasher works well with Inertia.js.
Installation
To use PHPFlasher with Inertia.js, install it the same way as in the Laravel Installation guide.
Also, add @flasher/flasher
to your package.json
:
"@flasher/flasher": "file:vendor/php-flasher/flasher/Resources"
Then, run:
npm install --force
Usage
Send notifications
from your HandleInertiaRequests
middleware.
<?php
// app/Http/Middleware/HandleInertiaRequests.php
class HandleInertiaRequests extends Middleware
{
public function share(Request $request): array
{
return array_merge(parent::share($request), [
'messages' => flash()->render('array'),
]);
}
}
Then, display your notifications
in your Layout.vue
file:
// resources/js/Shared/Layout.vue
<script>
import flasher from "@flasher/flasher";
export default {
props: {
messages: Object,
},
watch: {
messages(value) {
flasher.render(value);
}
}
}
</script>
Now, you can trigger notifications from anywhere in your application.
<?php
// app/Http/Controllers/UsersController.php
class UsersController
{
public function store()
{
// your saving logic
flash()->success('User created.');
return Redirect::route('users');
}
}