Laravel Notifications: Multi-Channel Delivery
Laravel Notifications: Multi-Channel Delivery
Laravel Notifications: Multi-Channel Delivery
Tip: Multiple Channels in One Notification
public function via($notifiable): array
{
return ['mail', 'database', 'slack'];
}
One notification class, multiple delivery methods.
Gotcha: toDatabase() Must Return Array
public function toDatabase($notifiable): array
{
return [
'message' => 'New order received',
'order_id' => $this->order->id,
];
}
Tip: On-Demand Notifications
Notification::route('mail', 'admin@example.com')
->notify(new InvoicePaid($invoice));
Send to anyone, not just notifiable models.
Gotcha: Notification Queueing
Add ShouldQueue to the notification class. Serialization happens at queue time, not send time.
Tip: Custom Notification Channels
public function via($notifiable): array
{
return [SmsChannel::class];
}
public function toSms($notifiable)
{
return (new SmsMessage())->content('Your code: 1234');
}
Gotcha: shouldSend() Method
public function shouldSend($notifiable, $channel): bool
{
return $notifiable->prefersNotification($channel);
}
Tip: Use route:cache Carefully
php artisan route:cache is fast, but it doesn't work with closure-based routes. Every time you cache routes, Laravel serializes them. If you have Route::redirect() or closure callbacks, the cache breaks. Stick to controller-based routes in production.
Tip: Model APP_KEY Rotation
Rotating APP_KEY invalidates all encrypted data — cookies, encrypted DB columns, and password reset tokens. If you must rotate (e.g., after a leak), plan a migration that re-encrypts existing data with the new key.
Gotcha: Local Scope Leaks
Global scopes defined in booted() apply to ALL queries on that model — including relationships. An innocent User::all() in admin panel might exclude soft-deleted users if a global scope is active.
Senior Insight
Multi-channel notifications are a killer feature, but they require careful design. The mistake I see: sending the same verbose message across all channels. Email can handle 500 words; SMS should be 160 characters. I design notification content per channel and use notification classes as orchestrators, not content generators. Also, always queue notifications — a slow email provider should never block an HTTP response.
Source: Laravel News (https://laravel-news.com/), Freek.dev (https://freek.dev/tags/laravel), Spatie Blog (https://spatie.be/blog)