0
0
Laravelframework~10 mins

Notification channels (mail, database, SMS) in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Notification channels (mail, database, SMS)
Trigger Notification Event
Notification Class Called
Determine Channels: mail, database, SMS
User Receives Notification via Chosen Channels
When a notification is triggered, Laravel calls the notification class, determines which channels to use, then sends the notification via mail, stores it in the database, or sends an SMS.
Execution Sample
Laravel
Notification::send($user, new InvoicePaid($invoice));

// In InvoicePaid Notification class
public function via($notifiable) {
  return ['mail', 'database', 'sms'];
}
This code sends a notification to a user via mail, database, and SMS channels.
Execution Table
StepActionChannelResultNotes
1Trigger Notification::sendAllNotification class InvoicePaid instantiatedStart sending notification
2Call via() methodAllChannels returned: mail, database, smsDetermines channels to send
3Send mailmailEmail sent to user email addressUses mail driver configured in Laravel
4Save notificationdatabaseNotification record saved in notifications tableStores data for user to view later
5Send SMSsmsSMS sent to user phone numberUses configured SMS gateway service
6CompleteAllUser receives notification via all channelsProcess ends
💡 All specified channels processed, notification delivery complete
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
$channelsnull['mail', 'database', 'sms']['mail', 'database', 'sms']['mail', 'database', 'sms']['mail', 'database', 'sms']['mail', 'database', 'sms']
$notificationStatusnot sentpendingmail sentmail sent, db savedmail sent, db saved, sms sentall sent
Key Moments - 3 Insights
Why does Laravel call the via() method before sending notifications?
Laravel calls via() to know which channels to use for sending the notification, as shown in step 2 of the execution_table.
How does Laravel handle sending notifications to multiple channels?
Laravel processes each channel one by one, sending mail, saving to database, and sending SMS separately, as seen in steps 3, 4, and 5.
What happens if one channel fails to send the notification?
Laravel attempts each channel independently; failure in one does not stop others. This is implied by separate steps for each channel.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what channels are returned by the via() method at step 2?
A['database', 'sms']
B['mail']
C['mail', 'database', 'sms']
D[]
💡 Hint
Check the 'Result' column at step 2 in the execution_table.
At which step is the notification saved to the database?
AStep 3
BStep 4
CStep 5
DStep 1
💡 Hint
Look for the 'database' channel action in the execution_table.
If the SMS channel was removed from via(), how would the execution_table change?
AStep 5 would be missing
BStep 3 would be missing
CStep 4 would be missing
DNo change
💡 Hint
Compare the channels listed in step 2 and the following steps.
Concept Snapshot
Laravel Notification Channels:
- Use via() method to specify channels like 'mail', 'database', 'sms'.
- Laravel sends notifications on each channel separately.
- Mail sends email, database stores notification, SMS sends text message.
- Channels run independently; failure in one doesn't stop others.
- Configure each channel in Laravel settings before use.
Full Transcript
In Laravel, when you want to notify a user, you create a notification class and define which channels to use in the via() method. When Notification::send is called, Laravel checks the channels and sends the notification through each one: mail sends an email, database saves a record, and SMS sends a text message. Each channel works independently, so if one fails, others still run. This process ensures users get notified in multiple ways depending on your app's setup.