Requirements for Livewire Alert
masterEnsure your environment meets the following minimum requirements:
- PHP: 8.1 or higher
- Laravel: 10.x or higher
- Livewire: 3.x or 4.x
- Composer
repository·master·Indexed 21 days ago
https://github.com/jantinnerezo/livewire-alertA Laravel Livewire package that integrates SweetAlert2 notifications into Livewire components using a fluent, chainable PHP API. It allows developers to trigger alerts, confirmation dialogs, and input fields directly from PHP, eliminating the need for manual JavaScript. Supports PHP 8.1+, Laravel 10.x+, and Livewire 3.x or 4.x, with specific integration support for Filament.
Ensure your environment meets the following minimum requirements:
If you are using Filament, you need to register SweetAlert2 as a Filament asset in your AppServiceProvider to ensure it is loaded correctly within the Filament admin panel.
use Filament\Support\Facades\FilamentAsset;
use Illuminate\Support\Facades\Vite;
use Filament\Support\Assets\Js;
public function boot()
{
FilamentAsset::register([
Js::make('sweetalert2', Vite::asset('resources/js/sweetalert2.js')),
// Or via CDN:
// Js::make('sweetalert2', 'https://cdn.jsdelivr.net/npm/sweetalert2@11'),
]);
}To use Livewire Alert in your Laravel project, follow these steps:
Install the package via Composer:
composer require jantinnerezo/livewire-alertPublish the configuration (optional):
php artisan vendor:publish --tag=livewire-alert:configInstall SweetAlert2: You must have SweetAlert2 available in your frontend. You can install it via npm/yarn or use a CDN.
Via npm/yarn:
npm install sweetalert2
# or
yarn add sweetalert2Then, import it in your resources/js/app.js to make it globally available:
import Swal from 'sweetalert2'
window.Swal = SwalVia CDN:
Include this script before the closing </body> tag:
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>composer require jantinnerezo/livewire-alertYou can publish the package configuration file to your application's config directory using the Artisan command. This allows you to customize the alert settings. Use the livewire-alert:config tag when publishing.
php artisan vendor:publish --tag=livewire-alert:configLivewire Alert provides a fluent, chainable API via the LivewireAlert facade. You can trigger different types of notifications (like success, error, warning, etc.) directly from your Livewire component methods.
use Jantinnerezo\LivewireAlert\Facades\LivewireAlert;
public function save()
{
LivewireAlert::title('Changes saved!')
->success()
->show();
}You can customize the visibility and appearance of the confirmation, cancel, and deny buttons:
withConfirmButton(?string $confirmButtonText = null): Shows the confirm button. Uses config value if text is null.withCancelButton(?string $cancelButtonText = null): Shows the cancel button.withDenyButton(?string $denyButtonText = null): Shows the deny button.confirmButtonText(string $text): Sets the text for the confirm button.cancelButtonText(string $text): Sets the text for the cancel button.denyButtonText(string $text): Sets the text for the deny button.confirmButtonColor(string $color): Sets the CSS color for the confirm button.cancelButtonColor(string $color): Sets the CSS color for the cancel button.denyButtonColor(string $color): Sets the CSS color for the deny button.reverseButtons(bool $reverse = true): Reverses the order of the buttons.You can attach custom actions to an alert that trigger when a user interacts with it. These actions are dispatched as Livewire events. Use the following methods to define these behaviors:
onConfirm(string $action, mixed $data = null): Triggers when the user confirms the alert.onDeny(string $action, mixed $data = null): Triggers when the user selects the deny option.onDismiss(string $action, mixed $data = null): Triggers when the alert is dismissed (e.g., clicking outside or closing).The $action string is the name of the event that will be dispatched, and $data is an optional payload sent with the event.
$this->alert()
->question('Are you sure?')
->onConfirm('delete-user', ['id' => 1])
->onDeny('cancel-delete')
->show();You can include an image in your alert using the following methods:
imageUrl(string $url): Sets the URL of the image.imageWidth(int $width): Sets the width of the image.imageHeight(int $height): Sets the height of the image.imageAlt(string $alt): Sets the alt text for the image.The LivewireAlert class is bound to the Laravel service container. You can resolve it using either the class name Jantinnerezo\LivewireAlert\LivewireAlert or the alias livewire-alert. The instance is automatically injected with the current Livewire component.
// Resolving via class name
$livewireAlert = app(\Jantinnerezo\LivewireAlert\LivewireAlert::class);
// Resolving via alias
$livewireAlert = app('livewire-alert');The LivewireAlert class provides a fluent API to configure and trigger SweetAlert2 notifications from within a Livewire component. You can chain methods to set the title, text, icon, position, and various other SweetAlert2 options before calling show() to display the alert.
Common configuration methods include:
title(string $title): Sets the alert title.text(string $text): Sets the alert body text.success(), error(), warning(), info(), question(): Sets the alert icon.toast(bool $toast): Configures the alert as a toast notification.asToast(): A shortcut for a toast notification positioned at TopEnd with a timer and progress bar.asConfirm(): A shortcut for a confirmation dialog with confirm and deny buttons.asLoading(string $title): A shortcut for a loading state alert.timer(int|null $timer): Sets the auto-close timer in milliseconds.position(Enums\Position|string $position): Sets the alert position.To display the alert, you must call show().
$this->alert()
->title('Success!')
->text('Your changes have been saved.')
->success()
->show();Livewire Alert supports various input types to collect data from the user via SweetAlert2. You can use the following methods to add inputs:
withTextInput(?string $label = null, ?string $placeholder = null, ?string $value = null)withEmailInput(?string $label = null, ?string $placeholder = null, ?string $value = null)withPasswordInput(?string $label = null, ?string $placeholder = null)withNumberInput(?string $label = null, ?string $placeholder = null, ?string $value = null)withTextareaInput(?string $label = null, ?string $placeholder = null, ?string $value = null)withSelectInput(array $options = [], ?string $label = null, ?string $value = null)withRadioInput(array $options = [], ?string $label = null, ?string $value = null)withCheckboxInput(?string $label = null, bool $checked = false)withFileInput(?string $label = null)$this->alert()
->title('Enter your name')
->withTextInput('Name', 'John Doe')
->onConfirm('submit-name')
->show();