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]);
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'));
}
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');