composer require ab01faz101/tail-alert
php artisan vendor:publish --provider="Ab01faz101\TailAlert\TailAlertServiceProvider"
resources/views/layouts/app.blade.php):
@livewireScripts
@tailalertScripts
use Ab01faz101\TailAlert\Facades\TailAlert;
public function save()
{
TailAlert::success('Data saved successfully!');
// or
TailAlert::error('Failed to save data.');
}
Triggering Alerts:
Use the TailAlert facade to dispatch alerts from any Livewire component:
TailAlert::info('This is an informational message.');
TailAlert::warning('This is a warning message.');
TailAlert::error('Something went wrong!');
TailAlert::success('Operation completed successfully.');
Customizing Alerts: Pass an array for additional options (e.g., duration, position, or custom icon):
TailAlert::success('Custom alert', [
'duration' => 5000, // 5 seconds
'position' => 'top-right',
'icon' => '✨',
]);
Conditional Alerts: Combine with Livewire's reactivity to show alerts based on state:
public $showSuccessAlert = false;
public function submitForm()
{
if ($this->validate()) {
$this->showSuccessAlert = true;
TailAlert::success('Form submitted!');
} else {
TailAlert::error('Validation failed.');
}
}
Integration with Livewire Events: Dispatch alerts in response to Livewire events:
protected $listeners = ['alertTriggered' => 'handleAlert'];
public function handleAlert($message)
{
TailAlert::info($message);
}
Dynamic Alerts in Blade: Display alerts in your Livewire component's Blade view:
@if(session()->has('status'))
@php
TailAlert::info(session('status'));
@endphp
@endif
Queueing Alerts:
Use Livewire's wire:ignore to queue alerts for batch processing:
<div wire:ignore>
@foreach($pendingAlerts as $alert)
{{ $alert }}
@endforeach
</div>
Theming:
Override Tailwind classes in the published config (config/tail-alert.php):
'classes' => [
'container' => 'max-w-md mx-auto',
'success' => 'bg-green-500 text-white',
'error' => 'bg-red-500 text-white',
],
Localization: Extend the package's language support by publishing and modifying the language file:
php artisan vendor:publish --tag=tail-alert-lang
Custom Alert Types:
Extend the TailAlert facade to support additional types (e.g., TailAlert::custom('Type', 'Message')):
// In a service provider or helper
if (!method_exists(\Ab01faz101\TailAlert\Facades\TailAlert::class, 'custom')) {
\Ab01faz101\TailAlert\Facades\TailAlert::macro('custom', function ($message, $options = []) {
return $this->dispatch('alert', [
'type' => 'custom',
'message' => $message,
'options' => $options,
]);
});
}
Missing @tailalertScripts:
Forgetting to include @tailalertScripts in your layout will result in alerts not rendering.
Fix: Ensure the script is included in your main layout file.
Alerts Not Showing: If alerts don’t appear, check:
TailAlert facade is correctly namespaced (use Ab01faz101\TailAlert\Facades\TailAlert;).Conflicting Tailwind Classes: Custom Tailwind classes in the config may conflict with your project’s existing styles. Tip: Use unique class names or inspect the generated HTML to debug.
Livewire Component Isolation:
Alerts triggered in a child Livewire component may not appear if the parent component’s script isn’t loaded.
Fix: Ensure the parent component includes @livewireScripts and @tailalertScripts.
Check the Network Tab: Look for failed requests or blocked scripts in your browser’s DevTools (Network tab).
Inspect Generated HTML: Open the browser’s DevTools (Elements tab) to verify the alert DOM structure is correct.
Log Dispatch Events:
Add a temporary dd() or Log::debug() to confirm the alert is being dispatched:
TailAlert::success('Debug message');
// Check Laravel logs for: "TailAlert dispatched: success"
Disable Caching: If alerts work in development but not production, clear caches:
php artisan view:clear
php artisan cache:clear
Custom Alert Templates: Override the Blade template by publishing and modifying:
php artisan vendor:publish --tag=tail-alert-views
Edit resources/views/vendor/tail-alert/alert.blade.php.
Add Animation: Extend the package to support animations by modifying the published JS:
php artisan vendor:publish --tag=tail-alert-scripts
Edit public/vendor/tail-alert/js/tail-alert.js.
Server-Side Alerts: Combine with Laravel’s session flash data for alerts that persist across requests:
session()->flash('status', 'This alert will persist!');
// In your Livewire component:
public function mount()
{
if (session()->has('status')) {
TailAlert::info(session('status'));
session()->forget('status');
}
}
Dark Mode Support:
Ensure your Tailwind config supports dark mode and update the alert classes in config/tail-alert.php:
'classes' => [
'dark' => 'dark:bg-gray-800 dark:text-white',
],
How can I help you explore Laravel packages today?