Laravel Notification System On Slack And Email

by Saquib Rizwan  July 13, 2017

In my previous articles, I helped you create a Laravel email sending system and a Contact Us form.

In this article, I will highlight another amazing feature of Laravel through a very basic Laravel notification system. Using this system, I will send a notification to the user by sending them an email and integrate Slack to setup the system for sending notifications on a Slack channel.

laravel notification system on slack and email

Laravel’s notification system is easy to implement because it send notifications by setting up a single class for each notification. This class describes how to notify users about the message using a particular channel.

Install Laravel

To quickly setup a Laravel application, sign up at Cloudways for free. Login to your account and create a new server. Fill in the server and the application details and select Laravel as your application. That’s it, you have just installed a functional Laravel application in just a few clicks on the Cloudways platform.

Create User Table

Now that application has been installed, I will create a User table. Launch the SSH terminal and login to your server using the Master Credentials.

Now go to the root of your application by typing the following commands:

Now that you have a default User table in your database, the next step is the addition of a record in it so that the system could send a notification via email to that user. Go to the Applications tab on the Cloudways platform and launch the Database Manager.

Add a simple record in the table and add a valid email id in it. The system will send the notification to this email address.  

Now go to app/User.php that has the following code:

If you look closely, you can see a Notifiable trait is been used. Whenever you want to make your model notifiable, all you have to do is to import use Illuminate\Notifications\Notifiable; trait in your model.

Just note that certain notification channels expects certain information available on the notifiable. For example, the mail channel expects the model to have an “email” property so it knows which email address to send the notification.

Create Notification For Email

For this example, I am keeping things very simple. A notification is generated whenever you get a visitor at the home page.

Go back to SSH, and in the root of your application, execute the following command:

By executing this command, you will have a Notification class. Go to app/Notifications/Newvisit.php. In this file you will find the following code:

Let’s understand this code. First, it has the constructor, where any relevant data will be injected.

Then, it has the via() method, which allows you to choose the notification method which will be used to send the notification to each individual instance.

Then, it have toMail() method, which returns three attributes. The first one is line, which specifies the starting body of the email. Then it has action, which specifies the button name and the URL to which the button will redirect. Then it has line again, which specifies the closing paragraph of the email. Here is a sample output:

email recieved

Send Email Notifications in Laravel  

Now go to routes/web.php and paste the following code in the file:

You would have to import notification class by use App\Notifications\Newvisit; then in the route function, I have called the first record from the User table which I inserted by $user = App\User::first(); then to send notification, I used the notify function and send the notification in Newvisit instance in the following line of code: $user->notify(new Newvisit(“A new user has visited on your application.”));

Now open app\Notifications\Newvisit.php and add the following code in it.

Next open .env file and set your database credentials and mailer function. Check out the Laravel Email article for details on this step. Your .env file should look like the following:

Now everything is ready. Go to the Application tab in the Cloudways platform and click the Launch Application button. You will be notified through an email.

cloudways application details

email received

Create Slack Notifications in Laravel

To create notification for Slack, you’ll need to bring Guzzle in via Composer. Go SSH and in the root of your application and run the following commands:

You will need a new class for Slack notifications. Go to app/Notifications/Newslack.php and paste the following code.

Here, via() method defines the medium for notification and toSlack() method sends the notification onto Slack.

Set Incoming Webhook

Now to receive Slack notification go to https://{yourteam}.slack.com/apps. Choose the “Incoming Webhook” type and add a new configuration.

slack webhook

Copy the Webhook URL and head back to your Laravel app.

Your model should implement a routeNotificationForSlack() method that returns this webhook. Therefore go to app/User.php  and add the following function in it.

Now go to routes/web.php and add the following routes.

Now go to the Application tab in the Cloudways platform, click the Launch Application button and add /slack in the url. You will receive Slack notification as shown below:

slack notification

Conclusion

In this tutorial, I created a Laravel notification system and how to send email and Slack notifications in Laravel applications.If you have any query feel free to comment below and Subscribe our blog for more Laravel tutorials.

Create Laravel apps without the worry of server management.

Deploy your app on optimized PHP hosting servers for Laravel.

About Saquib Rizwan

Saquib is a PHP Community Expert at Cloudways – A Managed PHP Hosting Cloud Platform. He is well versed in PHP and regularly contributes to open source projects. For fun, he enjoys gaming, movies and hanging out with friends. You can email him at saquib.rizwan@cloudways.com

Stay Connected:

You Might Also Like...