Notifications are a vital aspect when it comes to the development of a robust application. In this shot, you will learn a very basic way to send notifications to your users in Laravel.
You should already have your mail set up, as this method sends the notification via email.
Let’s use the User
model.
Go to your User
model and you will see use Notifiable
traits.
Execute the following command:
php artisan make:notification PostAdded
In the App
directory, you will see a new directory, Notifications
; this directory houses all of the notifications you will be adding. For now, open PostAdded.php
.
In via()
, you will see mail
which is what we’ll be using. toMail()
comes with a default mail, which you can edit to suit your application; for now, we’ll just leave the default.
Go to your Post
controller and add the following:
$user->notify(new \App\Notifications\PostAdded());
//after inserting the Post to the database
If your mail is set up properly, you have just notified the user with the default notification.
Notifications send a message through whichever channel you prefer (in this case, mail) to users whenever an action is performed. Notifications can also be used to monitor the activities of an end-user in an application. Laravel makes it easy to utilize and set up on your own.