Advanced
Dependency Injection
You can inject the alert builder directly into Livewire actions instead of using the facade.
Inject the alert
Type-hint Jantinnerezo\LivewireAlert\LivewireAlert on your component method and chain the same API.
Live demo
SweetAlert2
PHP
LivewireAlert
use Jantinnerezo\LivewireAlert\LivewireAlert;
public function save(LivewireAlert $alert)
{
$alert->title('Success!')
->text('What would you like to do?')
->question()
->withConfirmButton('Save')
->withCancelButton('Cancel')
->withDenyButton('Delete')
->onConfirm('saveFile', ['id' => $this->fileId])
->onDeny('deleteFile', ['id' => $this->fileId])
->onDismiss('cancelAction', ['id' => $this->fileId])
->show();
}
When to use it
Dependency injection is useful when you prefer explicit dependencies in actions or want to avoid facade calls in component methods.
- The injected class supports the same fluent methods as the facade.
- The alert still runs in the current Livewire component context.
- Use whichever style is clearer for the component you are writing.