Laravel (5.7) Notifications
Notifying users of various things that happen in your application is super easy to implement thanks to Laravel Notifications. Laravel notifications allow you to send a message to any user via Email, Slack, SMS, and many other channels.
Creating a Notification
To create a new notification you can run the following artisan command:
php artisan make:notification TestMessage
And this will create a new file located at app\Notifications\TestMessage.php with the following contents:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class TestMessage extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
Notice the via method in the code above. This is where you can specify how you want the user to be notified.In the example above we are specifying that we want the user to be notified through email:
return ['mail'];
Since we want them to be notified via mail, our notification will fire the toMail() method when you send a notification to a user.
If you wanted to include Slack notifications you would add 'slack' to the via() method:
return ['mail', 'slack'];
And then your notification would fire a toSlack() method:
public function toSlack($notifiable)
{
return (new SlackMessage)
->content('One of your invoices has been paid!');
}
There are many other notification channels that you can find out about at the official Laravel documentation:
https://laravel.com/docs/5.7/notifications
Sending a Notification
Using the Notifiable trait in your User class will allow you to easily send notifications to users:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
}
After adding that trait you can now run the following:
$user->notify(new App\Notifications\TestMessage);
And that particular user will be sent the TestMessage notification. Next if you wanted to send it to all your users you can use the Notification facade like so:
Notification::send($users, new App\Notifications\TestMessage);
Previous:
Laravel (5.7) Mail
Next:
Laravel (5.7) Package Development
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/laravel/notifications.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics