Installation:
composer require php-flasher/flasher-noty
Ensure php-flasher/flasher (≥2.5.1) is installed as a dependency.
Publish Config (Optional):
php artisan vendor:publish --provider="PHPFlasher\Flasher\FlasherServiceProvider" --tag="flasher-config"
Locate config at config/flasher.php under the noty section.
First Use Case:
// In a controller or Blade view
noty('User created successfully!', 'success');
This renders a Noty toast at the default position with the success theme.
config/flasher.php (Noty-specific settings like default_layout, theme).noty() helper in app/helpers.php (auto-loaded via FlasherServiceProvider).Flasher::queue() for managing notification order (e.g., during form submissions).// Controller
public function store(Request $request) {
$validated = $request->validate([...]);
noty('Record saved.', 'success');
return back();
}
@flasher directive.
@flasher
noty('Warning: Low disk space.', 'warning', [
'layout' => 'bottomRight',
'theme' => 'metroui',
'timeout' => 5000,
]);
config('flasher.noty.theme') to override globally.// Order notifications (e.g., for multi-step forms)
Flasher::queue('error', 'Invalid data.', ['timeout' => 2000]);
Flasher::queue('success', 'Data processed.');
Flasher::flush(); // Render all queued notifications
if ($user->save()) {
noty('Profile updated.', 'success');
} else {
noty('Update failed.', 'error', ['closeWith' => ['click']]);
}
Flasher::json() for API feedback:
return Flasher::json(['message' => 'Success'], 200, 'success');
Event::listen(UserCreated::class, function ($event) {
noty('New user registered: ' . $event->user->name, 'info');
});
notyOptions in config:
'notyOptions' => [
'onShow' => 'function() { console.log("Notification shown!"); }',
],
Queue Ordering:
Flasher::queue() render in FIFO order. Use Flasher::flush() explicitly to control timing.Flasher::flush() after all queued notifications are set.Theme Conflicts:
metroui, bootstrap_v4) require their CSS/JS. Ensure assets are loaded:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/noty@3.2.0/dist/noty.min.css">
<script src="https://cdn.jsdelivr.net/npm/noty@3.2.0/dist/noty.min.js"></script>
config('flasher.noty.theme') to standardize themes across the app.Session Dependence:
SESSION_DRIVER is configured (e.g., file, database).session()->get('flasher') for stored messages.JavaScript Errors:
notyOptions keys).notyOptions validation in config:
'notyOptions' => [
'timeout' => 3000, // Ensure valid keys
],
Log Notifications:
Add a middleware to log all noty() calls:
public function handle($request, Closure $next) {
if (app()->bound('flasher')) {
\Log::debug('Flasher:', app('flasher')->getMessages());
}
return $next($request);
}
Inspect Queued Messages:
dd(Flasher::getQueuedMessages()); // Debug queue state
Custom Noty Types: Extend Noty’s types by registering a new helper:
// app/Providers/AppServiceProvider.php
public function boot() {
Flasher::extend('custom', function ($message, $options = []) {
return noty($message, 'custom-type', $options);
});
}
Use via noty('Message', 'custom') or flasher('custom', 'Message').
Override Default Renderer: Replace Noty’s JavaScript renderer by publishing the view:
php artisan vendor:publish --provider="PHPFlasher\FlasherNoty\FlasherNotyServiceProvider" --tag="views"
Modify resources/views/vendor/flasher/noty.blade.php.
Queue Priorities: Implement priority-based queues:
Flasher::queue('error', 'Critical error!', ['priority' => 1]);
Flasher::queue('info', 'Info message.', ['priority' => 2]);
Tip: Sort queued messages by priority before flushing.
@if(session()->has('flasher'))
<script src="https://cdn.jsdelivr.net/npm/noty@3.2.0/dist/noty.min.js" defer></script>
@endif
2000ms) for non-critical messages to reduce UI clutter.How can I help you explore Laravel packages today?