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. 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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require mckenziearts/laravel-notify
    php artisan vendor:publish --tag=notify-config
    php artisan vendor:publish --tag=notify-assets
    
  2. 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 />
    
  3. Basic Usage:

    notify()->success()->title('Success!')->send();
    

First Use Case

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();
}

Implementation Patterns

Common Workflows

  1. Conditional Notifications:

    if ($user->save()) {
        notify()->success()->title('User updated')->send();
    } else {
        notify()->error()->title('Update failed')->send();
    }
    
  2. 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();
    
  3. 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();
    
  4. Dynamic Overrides:

    notify()
        ->preset('task-completed', ['title' => "Task #{$task->id} completed"])
        ->send();
    

Integration Tips

  • Form Requests: Use in handle() method of Form Requests for consistent feedback.
  • Service Classes: Encapsulate notification logic in services for reusability.
  • Middleware: Trigger notifications post-operation in middleware (e.g., HandleIncomingWebRequest).
  • API Responses: Combine with API responses for hybrid frontend/backend apps:
    return response()->json(['success' => true, 'message' => notify()->success()->title('API Success')->getMessage()]);
    

Gotchas and Tips

Pitfalls

  1. Asset Loading:

    • Forgetting to run npm run dev/npm run build after Tailwind integration.
    • Fix: Ensure @notifyCss and @notifyJs directives are in the correct <head>/</body> positions for non-Tailwind setups.
  2. Action Conflicts:

    • Mixing url() and action() on the same NotifyAction.
    • Fix: Use either url() (for navigation) or action() (for execution), not both.
  3. CSRF Issues:

    • Actions failing due to missing CSRF tokens.
    • Fix: Ensure @csrf directive is present in forms or use action() with proper route generation.
  4. Model Limitations:

    • Drake model doesn’t support actions.
    • Fix: Use Toast, Connect, Smiley, or Emotify for interactive notifications.
  5. Duration Overrides:

    • Global timeout (config/notify.php) may conflict with per-notification durations.
    • Fix: Explicitly set duration() in the notification chain if needed.

Debugging

  • Notifications Not Showing:

    • Check if <x-notify::notify /> is included in the layout.
    • Verify Alpine.js is loaded (required for dynamic behavior).
    • Tailwind: Ensure @source directive is correctly added to app.css.
  • Styling Issues:

    • For Tailwind, rebuild assets after adding @source.
    • For non-Tailwind, check public/vendor/mckenziearts/laravel-notify/dist/ for compiled assets.

Extension Points

  1. 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,
    ],
    
  2. 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;
        }
    }
    
  3. Preset Overrides: Dynamically override presets in runtime:

    $overrides = ['title' => 'Custom Title', 'message' => 'Custom Message'];
    notify()->preset('user-updated', $overrides)->send();
    
  4. 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());
    });
    

Performance Tips

  • Batch Notifications: For bulk operations, queue notifications to avoid UI lag:
    foreach ($users as $user) {
        notify()->success()->title("Processed {$user->name}")->queue();
    }
    
  • Lazy Loading: Defer notification rendering until user interaction (e.g., click a "Show Notifications" button) for high-traffic pages.
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.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata