mckenziearts/laravel-notify
Lightweight Laravel package for backend-driven toast notifications. Publish config/assets, drop in a Blade component, and trigger success/error/info messages from your app. Works great with Tailwind CSS and Alpine.js for smooth UI alerts.
Installation:
composer require mckenziearts/laravel-notify
php artisan vendor:publish --tag=notify-config
php artisan vendor:publish --tag=notify-assets
Tailwind Integration (Recommended):
@source directive in resources/css/app.css:
@source "../../vendor/mckenziearts/laravel-notify/resources/views/**/*.blade.php";
resources/js/app.js:
import Alpine from 'alpinejs';
window.Alpine = Alpine;
Alpine.start();
resources/views/layouts/app.blade.php):
<x-notify::notify />
npm run dev or npm run build.First Notification:
// In a controller method
notify()->success()->title('Success!')->send();
return back();
Display a success toast notification after a form submission:
public function store(Request $request) {
$validated = $request->validate([...]);
// Save data...
notify()->success()->title('Record saved')->send();
return back();
}
Notification Chain:
notify()
->model(NotificationModel::Toast) // Optional (defaults to Toast)
->type(NotificationType::Success) // success, error, warning, info
->title('Action completed')
->message('Optional details')
->duration(3000) // Optional (default: 5000ms)
->send();
Actionable Notifications:
notify()
->success()
->title('User deleted')
->actions([
NotifyAction::make()
->label('Undo')
->action(route('users.restore', $user->id)),
NotifyAction::make()
->label('View')
->url(route('users.index'))
->openUrlInNewTab(),
])
->send();
Preset Notifications:
// In config/notify.php
'preset-messages' => [
'user.created' => [
'type' => NotificationType::Success,
'title' => 'User created',
// ...
],
];
// Usage
notify()->preset('user.created')->send();
Form Requests: Use in FormRequest classes for validation feedback:
public function failedValidation(Validator $validator) {
notify()->error()->title('Validation failed')->send();
return back()->withErrors($validator);
}
Service Layer: Create a dedicated NotificationService to centralize logic:
class NotificationService {
public function success(string $title, ?string $message = null) {
notify()->success()->title($title)->message($message)->send();
}
}
Event Listeners: Trigger notifications on events:
public function handle(UserCreated $event) {
notify()->preset('user.created')->send();
}
API Responses: Use for API responses (with @notifyJs directive in Blade):
return response()->json(['success' => true], 200)
->header('X-Notify', json_encode([
'type' => 'success',
'title' => 'API Success',
]));
Dynamic Presets:
notify()->preset('user.created', [
'title' => "User {$user->name} created",
'message' => "ID: {$user->id}"
])->send();
Conditional Notifications:
if ($user->isAdmin()) {
notify()->info()->title('Admin action')->send();
} else {
notify()->success()->title('Action completed')->send();
}
Batch Processing:
foreach ($users as $user) {
notify()->preset('user.processed', [
'title' => "Processed {$user->email}"
])->send();
}
Asset Loading Issues:
npm run build.@notifyJs and @notifyCss directives are in the correct layout file (or Tailwind is configured to scan package views).Action Execution Failures:
Drake does not).Duration Overrides:
duration() not working as expected.config/notify.php) isn’t overriding your method call. Method calls take precedence.Preset Overrides:
notify()->preset('user.created', ['title' => 'New title'])->send();
Inspect the Queue:
dd(\Mckenziearts\Notify\Facades\Notify::queue()) to see pending notifications before send().Check the View:
<x-notify::notify /> to ensure it’s loaded.Alpine.js Conflicts:
app.js.Tailwind Scanning:
@source directive is in app.css.resources/views and vendor files.Custom Notification Models:
NotificationModel enum or create a new model class by implementing Mckenziearts\Notify\Contracts\NotificationModel.Custom Actions:
NotifyAction to add custom behavior:
class CustomAction extends NotifyAction {
public function customMethod() { ... }
}
Global Middleware:
public function handle($request, Closure $next) {
notify()->info()->title('Entering admin area')->send();
return $next($request);
}
Testing:
$this->expectsNotifications(1)
->notify()
->success()
->title('Test')
->send();
Default Timeout:
config/notify.php:
'timeout' => env('NOTIFY_TIMEOUT', 5000), // Default: 5000ms
Asset Paths:
public/vendor/mckenziearts/laravel-notify/dist/. Clear cached views if styles break after updates:
php artisan view:clear
Alpine.js Version:
package.json).Laravel Version:
Batch Notifications:
foreach ($items as $item) {
notify()->preset('item.processed')->send();
}
Disable Auto-Send:
queue() to defer sending until later:
notify()->success()->title('Pending')->queue();
// Later...
\Mckenziearts\Notify\Facades\Notify::sendQueued();
Minimize Actions:
How can I help you explore Laravel packages today?