Laravel Notification

Notifications are short informational messages that notify users of something that occurred in your application. Basically, Laravel provides support for sending notification across a variety of delivery channels, including mail, database, sms, markdown, broadcast and slack.

Creating Notifications in Laravel

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:

Step1: Install New Laravel Project

Create a new Laravel project with composer:
composer create-project –prefer-dist laravel/laravel LaravelNotification

Step2: Setup Database

Next, setup the database and mail driver and add the database and mail driver credentials to “.env” file.
Create Database:

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:

Step3: Create user table and Authentication

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

Step4: Create Notification:

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:

Step5: Send Notification:

Send Notification Using Notifiable Trait:
Open routes/web.php file add “Notifiable Trait” like this:

OR

Send Notification Using The Notification Facade:
Either you can send the notification by using the “Notification Facade” like this:

Step6: Next, Navigate The Browser:

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:

Leave a comment

Your email address will not be published. Required fields are marked *