├── resources ├── js │ └── toasts.js └── views │ └── toasts.blade.php ├── src ├── Components │ └── Toasts.php └── Providers │ └── LaravelLivewireToastsProvider.php ├── composer.json ├── config └── laravel-livewire-toasts.php └── readme.md /resources/js/toasts.js: -------------------------------------------------------------------------------- 1 | import {Toast} from 'bootstrap'; 2 | 3 | let toastsElement = document.getElementById('laravel-livewire-toasts'); 4 | 5 | Livewire.on('showBootstrapToast', () => { 6 | let toast = Toast.getOrCreateInstance(toastsElement); 7 | 8 | toast.show(); 9 | }); 10 | 11 | Livewire.hook('message.processed', (message) => { 12 | if (Object.keys(message.response.serverMemo.errors).length) { 13 | Livewire.emit('showToast', 'danger', toastsElement.dataset.errorMessage); 14 | } 15 | }); 16 | -------------------------------------------------------------------------------- /src/Components/Toasts.php: -------------------------------------------------------------------------------- 1 | color = $color; 22 | $this->message = $message; 23 | 24 | $this->emit('showBootstrapToast'); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /resources/views/toasts.blade.php: -------------------------------------------------------------------------------- 1 |
17 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bastinald/laravel-livewire-toasts", 3 | "homepage": "https://github.com/bastinald/laravel-livewire-toasts", 4 | "description": "Dynamic Laravel Livewire Bootstrap toasts.", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Kevin Dion", 9 | "email": "bastinald@icloud.com", 10 | "role": "Developer" 11 | } 12 | ], 13 | "require": { 14 | "laravel/framework": "^8.0", 15 | "livewire/livewire": "^2.0" 16 | }, 17 | "autoload": { 18 | "psr-4": { 19 | "Bastinald\\LaravelLivewireToasts\\": "src" 20 | } 21 | }, 22 | "extra": { 23 | "laravel": { 24 | "providers": [ 25 | "Bastinald\\LaravelLivewireToasts\\Providers\\LaravelLivewireToastsProvider" 26 | ] 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /config/laravel-livewire-toasts.php: -------------------------------------------------------------------------------- 1 | 3000, 16 | 17 | /* 18 | |-------------------------------------------------------------------------- 19 | | Error Message 20 | |-------------------------------------------------------------------------- 21 | | 22 | | This value is the error message to use for the toast that shows whenever 23 | | validation errors occur via Livewire components. This value can also be 24 | | changed by creating a translation for this value. 25 | | 26 | */ 27 | 28 | 'error_message' => 'Whoops! Something went wrong.', 29 | 30 | ]; 31 | -------------------------------------------------------------------------------- /src/Providers/LaravelLivewireToastsProvider.php: -------------------------------------------------------------------------------- 1 | loadViewsFrom(__DIR__ . '/../../resources/views', 'laravel-livewire-toasts'); 14 | 15 | $this->publishes( 16 | [__DIR__ . '/../../config/laravel-livewire-toasts.php' => config_path('laravel-livewire-toasts.php')], 17 | ['laravel-livewire-toasts', 'laravel-livewire-toasts:config'] 18 | ); 19 | 20 | $this->publishes( 21 | [__DIR__ . '/../../resources/views' => resource_path('views/vendor/laravel-livewire-toasts')], 22 | ['laravel-livewire-toasts', 'laravel-livewire-toasts:views'] 23 | ); 24 | 25 | Livewire::component('toasts', Toasts::class); 26 | } 27 | 28 | public function register() 29 | { 30 | $this->mergeConfigFrom(__DIR__ . '/../../config/laravel-livewire-toasts.php', 'laravel-livewire-toasts'); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Laravel Livewire Toasts 2 | 3 | This package allows you to dynamically show Bootstrap toasts via Laravel Livewire components. 4 | 5 | ## Documentation 6 | 7 | - [Requirements](#requirements) 8 | - [Installation](#installation) 9 | - [Usage](#usage) 10 | - [Showing Toasts](#showing-toasts) 11 | - [Emitting Events](#emitting-events) 12 | - [Publishing Assets](#publishing-assets) 13 | - [Custom Config](#custom-config) 14 | - [Custom View](#custom-view) 15 | 16 | ## Requirements 17 | 18 | - Bootstrap 5 must be installed via webpack first 19 | 20 | ## Installation 21 | 22 | Require the package: 23 | 24 | ```console 25 | composer require bastinald/laravel-livewire-toasts 26 | ``` 27 | 28 | Add the `livewire:toasts` component to your app layout view: 29 | 30 | ```html 31 |