Notification channels let your app send messages in different ways like email, SMS, or saving them in the database. This helps you reach users where they prefer.
Notification channels (mail, database, SMS) in Laravel
Start learning this pattern below
Jump into concepts and practice - no test required
public function via(object $notifiable): array
{
return ['mail', 'database', 'nexmo'];
}
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->line('Your notification message here.')
->action('View', url('/'))
->line('Thank you for using our app!');
}
public function toDatabase(object $notifiable): array
{
return [
'message' => 'Your notification message here.',
'url' => url('/'),
];
}
public function toNexmo(object $notifiable): NexmoMessage
{
return (new NexmoMessage)
->content('Your SMS notification message here.');
}The via method defines which channels to use for sending notifications.
Each channel has its own method like toMail, toDatabase, and toNexmo for SMS.
public function via(object $notifiable): array
{
return ['mail'];
}public function via(object $notifiable): array
{
return ['database'];
}public function via(object $notifiable): array
{
return ['mail', 'database', 'nexmo'];
}This notification sends a welcome message by email, saves it in the database, and sends an SMS to the user.
<?php namespace App\Notifications; use Illuminate\Notifications\Notification; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\NexmoMessage; class WelcomeUser extends Notification { public function via(object $notifiable): array { return ['mail', 'database', 'nexmo']; } public function toMail(object $notifiable): MailMessage { return (new MailMessage) ->line('Welcome to our app!') ->action('Get Started', url('/dashboard')) ->line('Thank you for joining us!'); } public function toDatabase(object $notifiable): array { return [ 'message' => 'Welcome to our app! Start exploring your dashboard.', 'url' => url('/dashboard'), ]; } public function toNexmo(object $notifiable): NexmoMessage { return (new NexmoMessage) ->content('Welcome to our app! Visit your dashboard to get started.'); } } // Usage example in a controller or event: // $user->notify(new WelcomeUser());
Make sure to configure your mail, database, and SMS services in Laravel before using these channels.
SMS channel uses services like Nexmo (now Vonage) or others; you need API keys set up.
Database notifications let users see alerts inside your app even if they miss emails or SMS.
Notification channels let you send messages in different ways easily.
Use via to pick channels, and define methods like toMail, toDatabase, and toNexmo.
Configure services properly to make notifications work smoothly.
Practice
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 namedvia.Final Answer:
via -> Option AQuick Check:
Channel selection method = via [OK]
- Confusing 'send' with 'via' method
- Using 'channels' instead of 'via'
- Assuming 'notify' defines channels
via method of a Laravel notification?Solution
Step 1: Check the return type of the via method
Theviamethod 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 CQuick Check:
Channel list must be an array of strings [OK]
- Returning a string instead of an array
- Calling a function instead of returning array
- Using incorrect channel class names
public function toDatabase($notifiable) {
return [
'invoice_id' => 1234,
'amount' => 250
];
}Solution
Step 1: Understand the toDatabase method role
ThetoDatabasemethod 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 BQuick Check:
toDatabase returns array data saved in DB [OK]
- Expecting a string instead of array
- Thinking only one key is saved
- Confusing toMail with toDatabase
public function via($notifiable) {
return ['nexmo'];
}
public function toNexmo($notifiable) {
return (new NexmoMessage)->content('Hello!');
}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
TheNexmoMessageclass must be imported withusestatement or aliased; missing import causes error.Final Answer:
NexmoMessage class is not imported or aliased -> Option DQuick Check:
Missing import causes class not found error [OK]
- Renaming toNexmo incorrectly
- Returning wrong channel name in via
- Misusing content method on NexmoMessage
via method implementations correctly sets all three channels?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 AQuick Check:
Use exact channel strings in via array [OK]
- Using 'sms' instead of 'nexmo'
- Using class names instead of strings
- Using shorthand like 'db' instead of 'database'
