Install the package:
composer require caiquebispo/notification-bell
php artisan vendor:publish --tag="notification-bell-config"
php artisan migrate
Add the trait to your User model:
use CaiqueBispo\NotificationBell\Traits\HasNotifications;
class User extends Authenticatable
{
use HasNotifications;
}
Include the Livewire component in your layout (e.g., resources/views/layouts/app.blade.php):
@auth
<livewire:notification-bell />
@endauth
Trigger your first notification (e.g., in a controller or job):
auth()->user()->success(
$targetUserId, // Can be the same as auth()->id() for self-notifications
'Welcome!',
'You have successfully registered.'
);
Start the queue worker (if using queued notifications):
php artisan queue:work
// In your registration controller
public function register(Request $request)
{
$user = User::create($request->validated());
// Send a success notification to the newly registered user
$user->success(
$user->id,
'Registration Complete',
'Your account has been created successfully. Welcome!'
);
return redirect('/dashboard');
}
Trigger:
NotificationHelper or the HasNotifications trait in your controllers, jobs, or events.// Using the helper
NotificationHelper::info($userId, 'System Update', 'New features are available.');
// Using the trait (directly on the User model)
auth()->user()->warning($userId, 'Low Storage', 'Your storage is running low.');
Queue Handling:
php artisan queue:work --daemon
php artisan queue:work without --daemon to see notifications process in real-time.Display:
notification-bell) automatically renders unread notifications in a dropdown.User Interaction:
/notifications (configurable).Listen to Laravel events and trigger notifications:
// In EventServiceProvider
protected $listen = [
'order.created' => [
function (OrderCreated $event) {
NotificationHelper::success(
$event->order->user_id,
'Order Created',
'Your order #' . $event->order->id . ' is confirmed.',
['order_id' => $event->order->id],
route('orders.show', $event->order)
);
},
],
];
Delay notifications using Laravel jobs (e.g., for email digests or batch processing):
// Create a job
php artisan make:job SendDelayedNotification
// In the job
public function handle()
{
NotificationHelper::info(
$this->userId,
'Scheduled Reminder',
'This is a delayed notification.'
);
}
// Dispatch the job
SendDelayedNotification::dispatch($userId)->delay(now()->addHours(1));
Pass context data (e.g., URLs, IDs) to notifications for actionable links:
auth()->user()->success(
$userId,
'Payment Received',
'Your payment of $' . $amount . ' was processed.',
['payment_id' => $payment->id], // Metadata
route('payments.show', $payment) // Action URL
);
The dropdown will render a clickable link to the payment page.
Send notifications to multiple users via Artisan or code:
// Via Artisan
php artisan notifications:send-bulk "System Maintenance" "The system will be down for maintenance." --users=1,2,3 --type=warning
// Via code
$admin = User::where('role', 'admin')->get();
NotificationHelper::create(
$admin->pluck('id')->toArray(),
'Admin Alert',
'A critical issue has been detected.'
);
Publish the views and override the Livewire component:
php artisan vendor:publish --tag="notification-bell-views"
Edit resources/views/vendor/notification-bell/notification-bell.blade.php to add custom buttons or fields.
Enable polling for instant updates (e.g., for collaborative apps):
// In config/notifications.php
'polling' => [
'enabled' => true,
'interval' => '5s', // Poll every 5 seconds
],
The Livewire component will automatically refresh unread counts.
Use Laravel Echo/Pusher for real-time updates (requires broadcasting setup):
// In config/notifications.php
'broadcasting' => [
'enabled' => true,
'channel' => 'notifications.{user_id}',
'event' => 'NotificationCreated',
],
Trigger broadcasts in your notification logic:
broadcast(new NotificationCreated($notification))->toOthers();
Enable browser sounds for alerts (note: requires user interaction first):
// In config/notifications.php
'features' => [
'sound' => [
'enabled' => true,
'volume' => 0.7,
],
],
The package auto-detects dark mode if your layout uses Tailwind’s dark classes:
<html class="dark">
No additional configuration is needed.
Queue Worker Requirement:
php artisan queue:work will result in silent failures.php artisan queue:work --daemon in production or test with sync driver in .env:
QUEUE_CONNECTION=sync
User Model Mismatch:
name for display. If your model uses nome (e.g., Brazilian systems), configure the column mapping:
// In config/notifications.php
'user_columns' => [
'name' => 'nome',
],
Livewire Scripts Missing:
@livewireScripts in your layout. Missing this will break the dropdown.@livewireScripts
Alpine.js Dependency:
<script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
Dark Mode Conflicts:
dark classes are applied to the root <html> element. The package relies on this for styling.Admin Panel Access:
config/notifications.php. Ensure your auth middleware is correctly configured.Check Queue Jobs:
php artisan queue:failed
php artisan queue:retry all
Log Notifications:
\Log::info('Notification triggered', [
'user_id' => $userId,
'title' => $title,
'message' => $message,
]);
Inspect Livewire State:
wire:debug to inspect the component’s state:
@livewire('notification-bell', key('notification-bell'))
wire:init and wire:poll).Database Issues:
notifications table exists and has the correct structure. Run:
php artisan migrate:fresh --seed
if migrations failed.Permission Denied:
HasNotifications trait.@auth).How can I help you explore Laravel packages today?