Laravel Notifications is an all new feature coming to Laravel 5.3 that allows you to make quick notification updates through services like Slack, SMS, Email, and more.
Let’s see how easy it is to build and send a notification using this new feature:
1class NewPost extends \Illuminate\Notifications\Notification 2{ 3 public function __construct($post) 4 { 5 $this->post = $post; 6 } 7 8 public function via($notifiable) 9 {10 return ['database'];11 }1213 public function toArray($notifiable)14 {15 return [16 'message' => 'A new post was published on Laravel News.',17 'action' => url($this->post->slug)18 ];19 }20}
All you need to do now is to send the notification to the selected users:
1$user->notify(new NewPost($post));
Creating Notifications
Laravel 5.3 ships with a new console command for creating notifications:
1php artisan make:notification NewPost
This will create a new class in app/Notifications
, each notification class contains a via()
method as well as different methods for building notifications for different channels.
Using the via()
method you can specifying the channels you’d like this particular notification to be sent through, check this example for the official documentation website:
1public function via($notifiable)2{3 return $notifiable->prefers_sms ? ['sms'] : ['mail', 'database'];4}
The via method receives a $notifiable
instance, which is the model you’re sending the notification to, in most cases it’ll be the user model but it’s not limited to that.
Available channels are: mail
, nexmo
, database
, broadcast
, and slack
.
Formatting Email Notifications
You can format how notifications are sent through different channels, for instance, let’s take a look at formatting a mail notification:
1public function toMail($notifiable)2{3 return (new MailMessage)4 ->subject('New Post!')5 ->line('A new post was published on Laravel News!')6 ->action('Read Post', url($this->post->slug))7 ->line('Please don\'t forget to share.');8}
This will create a mail message using a nice built-in responsive template that you can publish using the vendor:publish
console command.
The notification system will automatically look for an email
property in your model, however, you can customise this behaviour by defining a routeNotificationForMail
in your Model and return the email address this Model will be contacted on.
Formatting Nexmo SMS Notifications
Same as in the case of an email notification, you need to define a toNexmo
method in your notification class:
1public function toNexmo($notifiable)2{3 return (new NexmoMessage)4 ->from(123456789)5 ->content('New post on Laravel News!');6}
In the case of Nexmo notifications, laravel will look for a phone_number
property in the model. You can override that by defining a routeNotificationForNexmo
method.
You can set a global from
number in your Nexmo configuration file, that way you won’t have to provide a from
number in each notification.
Formatting Database Notifications
To Format a database notification you may define a toDatabase
method:
1public function toDatabase($notifiable)2{3 return new DatabaseMessage([4 'message' => 'A new post was published on Laravel News.',5 'action' => url($this->post->slug)6 ]);7}
To start using the database channel you may read the full documentation on the official website.
Sending Notifications
You can send notifications using the notify()
method on your model, this method exists on the Notifiable
trait which you’ll need to add to your Model.
1$user->notify(new NewPost($post));
You can also use the Notification facade, this will allow you to send notifications to multiple notifiables at the same time:
1Notification::send(User::all(), new NewPost($post));
Queueing Notifications
You can easily queue sending notifications by using the ShouldQueue
interface on the Notification class and including the Queueable
trait.
1<?php 2 3namespace App\Notifications; 4 5use Illuminate\Bus\Queueable; 6use Illuminate\Notifications\Notification; 7use Illuminate\Contracts\Queue\ShouldQueue; 8 9class NewPost extends Notification implements ShouldQueue10{11 use Queueable;12}