PHP code example of haxneeraj / laravel-livewire4-toaster

1. Go to this page and download the library: Download haxneeraj/laravel-livewire4-toaster 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/ */

    

haxneeraj / laravel-livewire4-toaster example snippets




namespace App\Livewire;

use Haxneeraj\LivewireToaster\Traits\Toastable;
use Livewire\Component;

class ProfileForm extends Component
{
    use Toastable;

    public string $name = '';

    public function save()
    {
        // ... validate and save ...

        $this->success('Profile updated successfully!', 'Saved');
    }

    public function delete()
    {
        // ... delete logic ...

        $this->error('Profile has been deleted.', 'Deleted');
    }

    public function archive()
    {
        $this->warning('Profile archived. You can restore it within 30 days.', 'Archived', [
            'duration' => 5000,
        ]);
    }

    public function notify()
    {
        $this->info('You have 3 unread messages.');
    }

    public function customToast()
    {
        $this->toast('success', 'Custom positioned toast!', null, [
            'position' => 'bottom-left',
            'duration' => 0, // sticky — manual dismiss only
        ]);
    }

    public function render()
    {
        return view('livewire.profile-form');
    }
}



namespace App\Http\Controllers;

use Haxneeraj\LivewireToaster\Facades\Toast;
use App\Http\Requests\UserRequest;

class UserController extends Controller
{
    public function store(UserRequest $request)
    {
        // ... create user ...

        Toast::success('User created successfully!', 'Welcome');

        return redirect()->route('users.index');
    }

    public function destroy(User $user)
    {
        $user->delete();

        Toast::error('User has been removed.', 'Deleted');

        return redirect()->route('users.index');
    }
}

// Quick toast
toast('success', 'Item saved!');

// Get the manager and chain
toast()->success('First toast')->error('Second toast');

// With options
toast('warning', 'Low disk space!', 'Warning', ['duration' => 10000]);

'styles' => [
    'success' => [
        'bg'          => 'bg-emerald-50 dark:bg-emerald-950/80',
        'border'      => 'border-emerald-200 dark:border-emerald-800',
        'text'        => 'text-emerald-800 dark:text-emerald-200',
        'icon_color'  => 'text-emerald-500 dark:text-emerald-400',
        'progress_bg' => 'bg-emerald-500',
    ],
    // ... error, warning, info
],

use Haxneeraj\LivewireToaster\Http\Middleware\InjectToasts;

return Application::configure(basePath: dirname(__DIR__))
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->web(append: [
            InjectToasts::class,
        ]);
    })
    ->create();

protected $middlewareGroups = [
    'web' => [
        // ... other middleware ...
        \Haxneeraj\LivewireToaster\Http\Middleware\InjectToasts::class,
    ],
];

use Livewire\Livewire;

public function test_profile_save_shows_success_toast()
{
    Livewire::test(ProfileForm::class)
        ->set('name', 'John')
        ->call('save')
        ->assertDispatched('toast');
}

use Haxneeraj\LivewireToaster\Facades\Toast;
use Haxneeraj\LivewireToaster\ToastManager;

public function test_user_creation_flashes_toast()
{
    $this->post('/users', ['name' => 'John', 'email' => '[email protected]']);

    $manager = app(ToastManager::class);
    // Toasts are flushed by middleware, so check session for redirect tests
    $this->assertNotNull(session('livewire_toaster'));
}

$this->success('Done!', 'OK', [
    'duration' => 5000,      // Override duration (ms)
    'position' => 'top-left', // Override position
]);
bash
php artisan vendor:publish --tag=toaster-config
javascript
// These work anywhere in your JS
Toast.success('Saved!');
Toast.error('Something went wrong.', 'Error');
Toast.info('FYI...', null, { duration: 5000 });
Toast.warning('Careful!', 'Warning');
Toast.show('success', 'Custom type toast');
bash
php artisan vendor:publish --tag=toaster-config
bash
php artisan vendor:publish --tag=toaster-views
js
// tailwind.config.js
module.exports = {
    content: [
        './resources/views/**/*.blade.php',
        './vendor/haxneeraj/livewire4-toaster/resources/views/**/*.blade.php',
        // ...
    ],
};