Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Notification channels (mail, database, SMS)
📖 Scenario: You are building a Laravel application that needs to notify users about important events. Notifications should be sent via email, stored in the database, and sent as SMS messages.
🎯 Goal: Create a Laravel notification class that sends notifications through mail, database, and SMS channels.
📋 What You'll Learn
Create a notification class named OrderShipped
Set up the notification to use mail, database, and nexmo channels
Define the toMail method with a subject and greeting
Define the toDatabase method to store order details
Define the toNexmo method to send an SMS message
💡 Why This Matters
🌍 Real World
Notifications keep users informed about important events like order shipments in e-commerce apps.
💼 Career
Understanding Laravel notifications is essential for backend developers working on user communication features.
Progress0 / 4 steps
1
Create the notification class
Create a Laravel notification class named OrderShipped that extends Notification and implements ShouldQueue. Import the necessary namespaces.
Laravel
Hint
Use php artisan make:notification OrderShipped to generate the class, then add implements ShouldQueue and use Queueable;.
2
Set notification channels
Add a via method that returns an array with 'mail', 'database', and 'nexmo' as the notification channels.
Laravel
Hint
The via method tells Laravel which channels to use for this notification.
3
Define the mail and database notification content
Add a toMail method that returns a MailMessage with subject 'Your order has shipped' and greeting 'Hello!'. Also add a toDatabase method that returns an array with 'order_id' => 1234 and 'status' => 'shipped'.
Laravel
Hint
The toMail method builds the email content. The toDatabase method returns data to store in the notifications table.
4
Add the SMS notification method
Add a toNexmo method that returns a NexmoMessage with content 'Your order #1234 has been shipped.'. Import Illuminate\Notifications\Messages\NexmoMessage.
Laravel
Hint
The toNexmo method defines the SMS message content using the NexmoMessage class.
Practice
(1/5)
1. In Laravel notifications, which method defines the channels a notification will be sent through?
easy
A. via
B. send
C. channels
D. notify
Solution
Step 1: Understand the notification class structure
Laravel notifications use a method to specify which channels to send the notification through.
Step 2: Identify the method name
The method that returns an array of channels is named via.
Final Answer:
via -> Option A
Quick Check:
Channel selection method = via [OK]
Hint: Remember: 'via' means 'through' which fits channel selection [OK]
Common Mistakes:
Confusing 'send' with 'via' method
Using 'channels' instead of 'via'
Assuming 'notify' defines channels
2. Which of the following is the correct syntax to specify the mail channel in the via method of a Laravel notification?
easy
A. return mail();
B. return 'mail';
C. return ['mail'];
D. return ['MailChannel'];
Solution
Step 1: Check the return type of the via method
The via method must return an array of channel names as strings.
Step 2: Identify the correct array syntax
Returning ['mail'] is correct; returning a string or function call is incorrect.
Final Answer:
return ['mail']; -> Option C
Quick Check:
Channel list must be an array of strings [OK]
Hint: Always return an array of channel names in via [OK]
Common Mistakes:
Returning a string instead of an array
Calling a function instead of returning array
Using incorrect channel class names
3. Given this Laravel notification code snippet, what will be stored in the database when the notification is sent?
public function toDatabase($notifiable) {
return [
'invoice_id' => 1234,
'amount' => 250
];
}
medium
A. Only the 'invoice_id' value 1234
B. An array with keys 'invoice_id' and 'amount' and their values
C. A JSON string of the notification class name
D. An error because toDatabase must return a string
Solution
Step 1: Understand the toDatabase method role
The toDatabase method returns an array of data saved in the notifications table.
Step 2: Analyze the returned array
The array contains keys 'invoice_id' and 'amount' with values 1234 and 250 respectively.
Final Answer:
An array with keys 'invoice_id' and 'amount' and their values -> Option B
Quick Check:
toDatabase returns array data saved in DB [OK]
Hint: toDatabase returns array data stored in notifications table [OK]
Common Mistakes:
Expecting a string instead of array
Thinking only one key is saved
Confusing toMail with toDatabase
4. Identify the error in this Laravel notification class snippet for sending SMS via Nexmo:
public function via($notifiable) {
return ['nexmo'];
}
public function toNexmo($notifiable) {
return (new NexmoMessage)->content('Hello!');
}
medium
A. Method name should be toSms, not toNexmo
B. The content method is not valid for NexmoMessage
C. The via method should return 'sms' instead of 'nexmo'
D. NexmoMessage class is not imported or aliased
Solution
Step 1: Check method names and channel names
Using 'nexmo' in via and method toNexmo is correct for SMS via Nexmo.
Step 2: Verify class usage
The NexmoMessage class must be imported with use statement or aliased; missing import causes error.
Final Answer:
NexmoMessage class is not imported or aliased -> Option D
Quick Check:
Missing import causes class not found error [OK]
Hint: Always import notification channel classes before use [OK]
Common Mistakes:
Renaming toNexmo incorrectly
Returning wrong channel name in via
Misusing content method on NexmoMessage
5. You want to send a Laravel notification via mail, database, and SMS (Nexmo). Which of the following via method implementations correctly sets all three channels?
hard
A. return ['mail', 'database', 'nexmo'];
B. return ['mail', 'database', 'sms'];
C. return ['MailChannel', 'DatabaseChannel', 'NexmoChannel'];
D. return ['mail', 'db', 'nexmo'];
Solution
Step 1: Recall correct channel names for Laravel notifications
Laravel uses lowercase strings 'mail', 'database', and 'nexmo' to specify channels.
Step 2: Compare options for correct channel names
return ['mail', 'database', 'nexmo']; uses correct channel names; others use incorrect or non-existent names.
Final Answer:
return ['mail', 'database', 'nexmo']; -> Option A
Quick Check:
Use exact channel strings in via array [OK]
Hint: Use exact lowercase channel names: mail, database, nexmo [OK]