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 (if applicable):
Add @source directive to resources/css/app.css:
@source "../../vendor/mckenziearts/laravel-notify/resources/views/**/*.blade.php";
Include the component in your layout:
<x-notify::notify />
Basic Usage:
notify()->success()->title('Success!')->send();
Trigger a success notification after a form submission:
public function store(Request $request) {
$validated = $request->validate([...]);
// Save logic here
notify()->success()->title('Record saved')->send();
return back();
}
Conditional Notifications:
if ($user->save()) {
notify()->success()->title('User updated')->send();
} else {
notify()->error()->title('Update failed')->send();
}
Actionable Notifications:
notify()
->success()
->title('Task completed')
->actions([
NotifyAction::make()->label('View')->url(route('tasks.show', $task->id)),
NotifyAction::make()->label('Edit')->action(route('tasks.edit', $task->id)),
])
->send();
Preset Notifications:
Define in config/notify.php:
'preset-messages' => [
'task-completed' => [
'type' => NotificationType::Success,
'model' => NotificationModel::Toast,
'title' => 'Task Completed',
'message' => 'Your task has been completed successfully.',
],
],
Use in controller:
notify()->preset('task-completed')->send();
Dynamic Overrides:
notify()
->preset('task-completed', ['title' => "Task #{$task->id} completed"])
->send();
handle() method of Form Requests for consistent feedback.HandleIncomingWebRequest).return response()->json(['success' => true, 'message' => notify()->success()->title('API Success')->getMessage()]);
Asset Loading:
npm run dev/npm run build after Tailwind integration.@notifyCss and @notifyJs directives are in the correct <head>/</body> positions for non-Tailwind setups.Action Conflicts:
url() and action() on the same NotifyAction.url() (for navigation) or action() (for execution), not both.CSRF Issues:
@csrf directive is present in forms or use action() with proper route generation.Model Limitations:
Drake model doesn’t support actions.Toast, Connect, Smiley, or Emotify for interactive notifications.Duration Overrides:
config/notify.php) may conflict with per-notification durations.duration() in the notification chain if needed.Notifications Not Showing:
<x-notify::notify /> is included in the layout.@source directive is correctly added to app.css.Styling Issues:
@source.public/vendor/mckenziearts/laravel-notify/dist/ for compiled assets.Custom Models:
Extend Mckenziearts\Notify\Enums\NotificationModel to add new notification types:
namespace App\Enums;
use Mckenziearts\Notify\Enums\NotificationModel as BaseModel;
enum CustomModel: string {
case MyCustomModel = 'my-custom-model';
}
Register in config/notify.php:
'models' => [
BaseModel::class,
App\Enums\CustomModel::class,
],
Custom Actions:
Extend Mckenziearts\Notify\Action\NotifyAction for specialized behavior (e.g., modal triggers):
class CustomAction extends NotifyAction {
public function modal() {
$this->classes('bg-blue-500');
return $this;
}
}
Preset Overrides: Dynamically override presets in runtime:
$overrides = ['title' => 'Custom Title', 'message' => 'Custom Message'];
notify()->preset('user-updated', $overrides)->send();
Event Listeners:
Listen for notification events (e.g., Mckenziearts\Notify\Events\NotificationSent):
use Mckenziearts\Notify\Events\NotificationSent;
NotificationSent::listen(function ($notification) {
Log::info('Notification sent:', $notification->toArray());
});
foreach ($users as $user) {
notify()->success()->title("Processed {$user->name}")->queue();
}
How can I help you explore Laravel packages today?