PHP code example of akhaled / livewire-sweetalert

1. Go to this page and download the library: Download akhaled/livewire-sweetalert 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/ */

    

akhaled / livewire-sweetalert example snippets


use Akhaled\LivewireSweetalert\Toast;
use Livewire\Component;

class MyComponent extends Component
{
    use Toast;

    public function save() {
        $this->toast('Toast message', 'success', 5000);
    }
    ...
}

use Akhaled\LivewireSweetalert\Fire;
use Livewire\Component;

class MyComponent extends Component
{
    use Fire;

    public function save() {
        $options = [];
        $this->Fire('Error happened', 'error', 'please try again later', $options);
    }
    ...
}

use Akhaled\LivewireSweetalert\Confirm;
use Livewire\Component;

class MyComponent extends Component
{
    use Confirm;

    protected $listeners = [
        'confirmed' => 'onConfirmation'
    ];

    public function delete()
    {
        $options = []; // default ['event' => 'confirmed']
        $this->confirm('Are you sure you want to delete', 'you can\'t revert that', $options);
    }

    public function onConfirmation()
    {
        dd('confirmed!');
    }
}

use Akhaled\LivewireSweetalert\Confirm;
use Livewire\Component;

class MyComponent extends Component
{
    use Confirm;

    protected $listeners = [
        'confirmed' => 'onConfirmation',
        'anotherConfirmed' => 'onAnotherConfirmation'
    ];

    public function delete()
    {
        $options = []; // default ['event' => 'confirmed']
        $this->confirm('Are you sure you want to delete', 'you can\'t revert that', $options);
    }

    public function onConfirmation()
    {
        dd('confirmed!');
    }

    public function anotherAction()
    {
        $options = [
            'event' => 'anotherConfirmed'; // <-- that's how it works!
        ];
        $this->confirm('Are you sure you want to delete', 'you can\'t revert that', $options);
    }

    public function onAnotherConfirmation()
    {
        dd('confirmed #2!');
    }
}

use Akhaled\LivewireSweetalert\Confirm;
use Livewire\Component;
use Livewire\Attributes\On;

class MyComponent extends Component
{
    use Confirm;

    public function confirmedWithAttribute()
    {
        $options = [
            'event' => 'phpOnAttribute';
        ];
        $this->confirm('Are you sure you want to delete', 'you can\'t revert that', $options)
    }

    #[On('phpOnAttribute')]
    public function onConfirmationWithAttribute()
    {
        dd('confirmed #3!');
    }
}

use Akhaled\LivewireSweetalert\Confirm;
use Livewire\Component;
use Livewire\Attributes\On;

class MyComponent extends Component
{
    use Confirm;

    public function save()
    {
        $this->confirm(
            event: 'savedConfirmed',
            data: [
                'key' => 'value',
            ]
        )
    }

    #[On('savedConfirmed')]
    public function onSavedConfirmations(array $data)
    {
        dd($data['key']); // value
    }
}