How to make PHPFlasher work with Inertia.js ?

Last updated at 08 May 2022 | Published at 29 August 2021

If you like PHPFlasher please consider giving it a on github or by spreading the word on twitter. Your support allows me to keep this package up-to-date and maintainable. Thank you


PHPFlasher offers a solid integration with Inertia.js

Installation

Please follow the same installation steps as for the Laravel Installation package.

You have also to install @flasher/flasher from npm.

npm i @flasher/flasher

Usage

Dispatch notifications from your HandleInertiaRequests middleware shared data.

<?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 render your notifications from your Layout.vue file like the following:

// resources/js/Shared/Layout.vue
<script>
import flasher from "@flasher/flasher";

export default {
  props: {
    messages: Object,
  },
  watch: {
    messages(value) {
      flasher.render(value);
    }
  }
}
</script>

All you have to do now, is to trigger you notification from anywhere in your application.

<?php
// app/Http/Controllers/UsersController.php
class UsersController
{
    public function store()
    {
        // your saving logic
        
        flash()->addSuccess('User created.');
        
        return Redirect::route('users');
    }
}
Younes

PHPFlasher is a project by Younes KHOUBZA.