Files
php-flasher/docs/pages/inertia.md
T
2024-09-21 23:15:00 +01:00

1.9 KiB

permalink, title, description
permalink title description
/inertia/ Inertia Easily add flash notification messages to your Inertia application with PHPFlasher. Follow our step-by-step guide to install and use the library in your project, and start engaging and informing your users with powerful flash messages.

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');
    }
}