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.
0
0
Notification channels (mail, database, SMS) in Laravel
Introduction
Send an email alert when a user signs up.
Save notifications in the database for users to see later.
Send SMS messages for urgent alerts or two-factor authentication.
Notify users via multiple channels at once for important updates.
Syntax
Laravel
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.
Examples
This example sends the notification only by email.
Laravel
public function via(object $notifiable): array
{
return ['mail'];
}This example saves the notification in the database only.
Laravel
public function via(object $notifiable): array
{
return ['database'];
}This example sends the notification by email, saves it in the database, and sends an SMS.
Laravel
public function via(object $notifiable): array
{
return ['mail', 'database', 'nexmo'];
}Sample Program
This notification sends a welcome message by email, saves it in the database, and sends an SMS to the user.
Laravel
<?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());
OutputSuccess
Important Notes
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.
Summary
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.