Intermediate LevelLaravel
How does Laravel Notification system dispatch multi-channel alerts?
❓ Interview Prompt: Explain Notification class (php artisan make:notification), via() channel selection, toMail(), toDatabase(), and toSlack().
Laravel Notifications send short informational alerts across multiple delivery channels (Mail, Database, SMS, Slack, WebSockets).
Structure:
•
'via($notifiable)': Returns array of active channels: '['mail', 'database', 'slack']'.
•
Representation Methods: Implements 'toMail()', 'toDatabase()', 'toSlack()' for each channel.
•
Dispatching: Execute '$user->notify(new InvoicePaid($invoice))' or 'Notification::send($users, new InvoicePaid($invoice))'.
Code Implementation Example
php
<?php
namespace AppNotifications;
use IlluminateBusQueueable;
use IlluminateNotificationsNotification;
use IlluminateNotificationsMessagesMailMessage;
class OrderConfirmed extends Notification
{
use Queueable;
public function via(object $notifiable): array {
return ['mail', 'database'];
}
public function toMail(object $notifiable): MailMessage {
return (new MailMessage)->line('Order confirmed!')->action('View Order', url('/orders'));
}
public function toDatabase(object $notifiable): array {
return ['message' => 'Order confirmed successfully'];
}
}💡Multi-channel Notification sending Mail and storing Database notifications.
🎯 Interview Key Takeaway
Use Notification classes to dispatch alerts across multiple channels (Mail, Database, Slack) simultaneously.
#Notifications#Mail#Database Notifications#Intermediate
Back to all Laravel Questions→