In Laravel, each notification is represented by a single class and stored in the “app/Notifications” directory. This directory will be created for you when you run the “make:notification” Artisan command in your terminal.
Basically Laravel feature provides the notification system; therefore, we just have to configure it on our Laravel project. In this article, we will show how to add notification system in Laravel. So, just follow these steps:
Create a new Laravel project with composer:
composer create-project –prefer-dist laravel/laravel LaravelNotification
Next, setup the database and mail driver and add the database and mail driver credentials to “.env” file.
database credentials to .env file:
Browse the mailtrap.io and create your own id, then you will get your own credentials like this.
Add your Username and Password to .env file like this:
For creating the users table run the artisan command:
“php artisan migrate”.
Next, add authentication using auth command:
“php artisan make:auth”
Register the user in users table, navigate your browser localhost:8000/register
Next, we need to add notification in your Laravel project. So run the artisan command:
“php artisan make:notification SendNotification”
After running the command, the file will be generated at app/Notification/SendNotification.php directory:
Send Notification Using Notifiable Trait:
Open routes/web.php file add “Notifiable Trait” like this:
Send Notification Using The Notification Facade:
Either you can send the notification by using the “Notification Facade” like this:
After configuring the notification, navigate the URL: 127.0.0.1:8000/ or localhost:8000 and then check the demo inbox in mailtrap.io you will get the notification like this:
Tags: Laravel Notification