Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Notify Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require mckenziearts/laravel-notify
    php artisan vendor:publish --tag=notify-config --tag=notify-assets
    
  2. Tailwind Integration (recommended for modern projects):

    • Add @source directive in resources/css/app.css:
      @source "../../vendor/mckenziearts/laravel-notify/resources/views/**/*.blade.php";
      
    • Import Alpine.js in resources/js/app.js:
      import Alpine from 'alpinejs';
      window.Alpine = Alpine;
      Alpine.start();
      
    • Add component to layout (resources/views/layouts/app.blade.php):
      <x-notify::notify />
      
  3. First Notification:

    // In a controller before redirect
    notify()->success()->title('Success!')->send();
    return back();
    

Key First Use Cases

  • Basic Success/Error Feedback:
    notify()->error()->title('Failed')->message('Something went wrong')->send();
    
  • Actionable Notifications (e.g., undo operations):
    notify()->success()->title('Deleted')
        ->actions([NotifyAction::make()->label('Undo')->action(route('undo'))])
        ->send();
    

Implementation Patterns

Core Workflow

  1. 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();
    }
    
  2. Service Layer Pattern:

    // In a service class
    public function createData() {
        $data = Data::create([...]);
        notify()->preset('data-created', ['title' => 'New Data']);
        return $data;
    }
    
  3. Form Request Handling:

    public function handle() {
        if ($this->fails()) {
            notify()->error()->title('Validation Error')->message($this->errors()->first());
            return back();
        }
        // Success logic
    }
    

Advanced Patterns

  • 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()
    ]);
    

Integration Tips

  1. View Composers:

    // Share notifications across views
    View::composer('*', function ($view) {
        $view->with('notifications', notify()->getQueue());
    });
    
  2. Event Listeners:

    // app/Listeners/NotifyOnModelEvent.php
    public function handle() {
        notify()->success()->title('Event Triggered')->send();
    }
    
  3. Middleware:

    public function handle($request, Closure $next) {
        if ($request->wantsJson()) {
            notify()->json()->title('API Info')->send();
        }
        return $next($request);
    }
    

Gotchas and Tips

Common Pitfalls

  1. Asset Loading Issues:

    • Problem: Notifications not appearing after npm run build.
    • Fix: Ensure @notifyCss and @notifyJs directives are in the correct layout sections or Tailwind is configured to scan package views.
  2. Action Execution Failures:

    • Problem: Actions not triggering properly.
    • Debug: Verify:
      • Correct model type (Drake doesn't support actions)
      • Proper route naming
      • CSRF protection (handled automatically)
  3. Preset Overrides:

    • Problem: Overrides not working.
    • Fix: Use exact config keys:
      notify()->preset('key', ['title' => 'New Title']) // Not 'title' => 'New Title'
      

Debugging Tips

  1. Queue Inspection:

    // Dump all queued notifications
    dd(notify()->getQueue());
    
  2. Model-Specific Issues:

    • Drake Model: Only supports success/error types (no custom messages)
    • Toast Model: Supports all features including actions
  3. Duration Quirks:

    • Set duration(0) to make notifications persistent
    • Default timeout can be set in config/notify.php

Extension Points

  1. Custom 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();
    
  2. Custom Actions:

    // Extend NotifyAction class
    class CustomAction extends NotifyAction {
        public function customMethod() { /* ... */ }
    }
    
    // Usage
    notify()->actions([new CustomAction()])->send();
    
  3. Configuration Overrides:

    // Temporarily override config
    notify()->config(['timeout' => 10000])->send();
    

Performance Considerations

  1. Queue Management:

    • Notifications are stored in session by default
    • For high-traffic apps, consider:
      notify()->queue()->send(); // Store in DB instead of session
      
  2. Asset Optimization:

    • Pre-compile assets for production:
      php artisan notify:compile
      
  3. Memory Usage:

    • Clear old notifications periodically:
      notify()->clear();
      

Configuration Quirks

  1. Tailwind Scanning:

    • If notifications don't style correctly, ensure:
      @tailwind base;
      @tailwind components;
      @tailwind utilities;
      @source "../../vendor/mckenziearts/laravel-notify/resources/views/**/*.blade.php";
      
  2. Alpine.js Conflicts:

    • If Alpine.js isn't working, verify:
      • Proper import in app.js
      • No duplicate Alpine instances
  3. Session Driver:

    • Notifications require session driver (default: file or database)

Pro Tips

  1. Notification Testing:

    // Test notifications in PHPUnit
    $this->actingAs($user)
         ->post('/endpoint')
         ->assertSessionHas('notify');
    
  2. Localization:

    // In config/notify.php
    'locale' => app()->getLocale(),
    // Then use __() in notification views
    
  3. 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>
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport