mckenziearts/laravel-notify
Lightweight Laravel package for backend-driven toast notifications. Install via Composer, publish config/assets, drop in the Blade component, and trigger success/error/info messages from your app. Works great with Tailwind CSS + Alpine.js.
Installation:
composer require mckenziearts/laravel-notify
php artisan vendor:publish --tag=notify-config --tag=notify-assets
Tailwind Integration (recommended for modern projects):
@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 />
First Notification:
// In a controller before redirect
notify()->success()->title('Success!')->send();
return back();
notify()->error()->title('Failed')->message('Something went wrong')->send();
notify()->success()->title('Deleted')
->actions([NotifyAction::make()->label('Undo')->action(route('undo'))])
->send();
Controller Integration:
public function update(Request $request, $id) {
$model = Model::findOrFail($id);
$model->update($request->all());
notify()
->success()
->title('Updated')
->message('Record #'.$id.' saved')
->send();
return redirect()->back();
}
Service Layer Pattern:
// In a service class
public function createData() {
$data = Data::create([...]);
notify()->preset('data-created', ['title' => 'New Data']);
return $data;
}
Form Request Handling:
public function handle() {
if ($this->fails()) {
notify()->error()->title('Validation Error')->message($this->errors()->first());
return back();
}
// Success logic
}
Dynamic Presets:
// In config/notify.php
'preset-messages' => [
'user.{action}' => [
'model' => NotificationModel::Toast,
'title' => 'User {action}',
'message' => 'Action completed for user #{$id}',
],
];
// Usage
notify()->preset('user.deleted', ['id' => $user->id])->send();
Conditional Notifications:
if ($user->isAdmin()) {
notify()->model(NotificationModel::Drake)->success()->send();
} else {
notify()->success()->title('Updated')->send();
}
API Response Wrapping:
return response()->json([
'success' => true,
'message' => notify()->success()->title('API Success')->getMessage()
]);
View Composers:
// Share notifications across views
View::composer('*', function ($view) {
$view->with('notifications', notify()->getQueue());
});
Event Listeners:
// app/Listeners/NotifyOnModelEvent.php
public function handle() {
notify()->success()->title('Event Triggered')->send();
}
Middleware:
public function handle($request, Closure $next) {
if ($request->wantsJson()) {
notify()->json()->title('API Info')->send();
}
return $next($request);
}
Asset Loading Issues:
npm run build.@notifyCss and @notifyJs directives are in the correct layout sections or Tailwind is configured to scan package views.Action Execution Failures:
Preset Overrides:
notify()->preset('key', ['title' => 'New Title']) // Not 'title' => 'New Title'
Queue Inspection:
// Dump all queued notifications
dd(notify()->getQueue());
Model-Specific Issues:
Duration Quirks:
duration(0) to make notifications persistentconfig/notify.phpCustom Notification Models:
// Create new model in app/Notifications/Models/CustomModel.php
namespace App\Notifications\Models;
use Mckenziearts\Notify\Contracts\NotificationModel;
class CustomModel implements NotificationModel {
public function view() { return 'custom.notify'; }
}
// Usage
notify()->model(new CustomModel())->send();
Custom Actions:
// Extend NotifyAction class
class CustomAction extends NotifyAction {
public function customMethod() { /* ... */ }
}
// Usage
notify()->actions([new CustomAction()])->send();
Configuration Overrides:
// Temporarily override config
notify()->config(['timeout' => 10000])->send();
Queue Management:
notify()->queue()->send(); // Store in DB instead of session
Asset Optimization:
php artisan notify:compile
Memory Usage:
notify()->clear();
Tailwind Scanning:
@tailwind base;
@tailwind components;
@tailwind utilities;
@source "../../vendor/mckenziearts/laravel-notify/resources/views/**/*.blade.php";
Alpine.js Conflicts:
app.jsSession Driver:
file or database)Notification Testing:
// Test notifications in PHPUnit
$this->actingAs($user)
->post('/endpoint')
->assertSessionHas('notify');
Localization:
// In config/notify.php
'locale' => app()->getLocale(),
// Then use __() in notification views
Dark Mode Support:
<!-- In your notification view -->
<div x-data="{ dark: window.matchMedia('(prefers-color-scheme: dark)').matches }"
:class="{ 'bg-gray-800': dark }">
<!-- Content -->
</div>
How can I help you explore Laravel packages today?