1. Go to this page and download the library: Download tuhin-su/livewire-swal library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tuhin-su / livewire-swal example snippets
namespace App\Livewire;
use Livewire\Component;
use LaravelSwal\Traits\Swal;
class MyComponent extends Component
{
use Swal;
public function save()
{
$this->swalToastSuccess('Saved', 'Record updated successfully.');
}
}
'auto_inject' => false,
namespace App\Livewire;
use Livewire\Component;
use LaravelSwal\Traits\Swal;
class MyComponent extends Component
{
use Swal;
public function save()
{
$this->swalToastSuccess('Saved', 'Record updated successfully.');
}
}
// Dispatch "users.delete" with user ID if confirmed
$this->swalConfirm(
title: 'Delete user?',
text: 'This action cannot be undone.',
opts: ['confirmButtonText' => 'Delete', 'cancelButtonText' => 'Cancel'],
thenEvent: 'users.delete',
thenParams: ['id' => $id]
);
use Livewire\Attributes\On;
#[On('users.delete')]
public function delete(array $payload)
{
$id = (int)($payload['id'] ?? 0);
// Perform delete...
$this->swalToastSuccess('Deleted', "User {$id} has been removed.");
}
use Livewire\Attributes\On;
#[On('notes.saved')]
public function saveNote(array $payload)
{
$note = (string)($payload['value'] ?? '');
// Save note logic...
$this->swalToastSuccess('Saved', 'Your note was recorded.');
}
$this->swalSecureAction(
string $method, // Target protected/private method to execute
array $params = [], // Parameters to pass to the method
bool $ 'Please confirm your password to continue',
array $passwordOpts = []
);
class Settings extends Component
{
use Swal;
public function render()
{
// Generate the cryptographically signed token
$deletePayload = $this->swalGenerateSecureActionPayload(
method: 'deleteAccount',
params: ['id' => 5],
use LaravelSwal\Traits\Swal;
use Livewire\Component;
class UserManagement extends Component
{
use Swal;
public function initiateDelete($userId)
{
// 1. Asks user to confirm deletion (Yes/No)
// 2. Prompts user for their password
// 3. Verifies password on the server and executes the protected `deleteUser` method
$this->swalSecureAction(
method: 'deleteUser',
params: ['id' => $userId],
afely
$user = User::findOrFail($id);
$user->delete();
$this->swalToastSuccess('Deleted', 'User removed successfully.');
}
}