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 seamlessly with Livewire v3.
Requirements
PHP v8.2 or higher Laravel v11.0 or higher
Installation
To use PHPFlasher with Livewire, follow the same installation steps as in the Laravel Installation guide.
composer require php-flasher/flasher-laravel
After installing, run this command to set up the required assets:
php artisan flasher:install
Usage
Dispatch notifications
from your components
#/ livewire
namespace App\Livewire;
use Livewire\Component;
class UserComponent extends Component
{
public function save()
{
flash()->success('User saved successfully!');
}
public function render()
{
return view('livewire.user');
}
Events
For SweetAlert, you can listen to sweetalert:confirmed
, sweetalert:denied
, and sweetalert:dismissed
events within your component.
#/ livewire events
namespace App\Livewire;
use Livewire\Attributes\On;
use Livewire\Component;
class UserComponent extends Component
{
public function render()
{
return <<<'HTML'
<div>
<button wire:click="deleteUser">Delete User</button>
</div>
HTML;
}
public function deleteUser()
{
sweetalert()
->showDenyButton()
->info('Are you sure you want to delete the user ?');
}
#[On('sweetalert:confirmed')]
public function onConfirmed(array $payload): void
{
flash()->info('User successfully deleted.');
}
#[On('sweetalert:denied')]
public function onDeny(array $payload): void
{
flash()->info('Deletion cancelled.');
}
}
event handlers context
Each listener method accepts an array $payload parameter, which contains:
public function sweetalertConfirmed(array $payload)
{
$promise = $payload['promise'];
$envelope = $payload['envelope'];
}
- promise: The resolved promise from SweetAlert.
- envelope: The notification where the event happened.