Laravel: Notification to Slack
Assalamualaikum / Hi,
Today I will show you how to make a notification to Slack channel.
The approach slightly different you may came across on the internet, but they key point, still rely on Laravel Notification. It’s just how I manage my Slack service.
First, we going to start with setting up our environment for Slack Notification by adding the Slack service configuration. Add new services in
Then create a webhook URL in Incoming Webhooks.
SLACK_WEBHOOK_URL, and add the webhook you just created above.
Once you’re done, in your terminal, don’t forget to run
php artisan config:cache.
Next step is to setup our Slack service and helper.
The service basically it’s just a simple class to have a webhook URL store in it, and a helper make it simpler to call the Slack service.
We going to create a service class in
Then create a helper in
app/Support, call it
helpers.php. Then add the following and then make sure in
composer.json, to load the
composer dumpautoload -o once you have added the helper.
Laravel provide a trait
IlluminateNotificationsNotifiable, which you can use it anywhere in any classes you want.
In our case, I want to create a class called
Did you notice how I use
slack() helper to get the Slack Webhook URL? Much convenient. You may want to extend the Slack service class in future, but in our case, we don’t need any as for now.
But again, you can simplify that by using
env() function to get the Slack Webhook URL instead of having configuration, helper and Slack service class.
This is just my practice when dealing with 3rd party service – configuration, service class and a helper.
Don’t forget to
composer dumpautoload -o once created the class.
Do take note the
routeNotificationForSlack(), that is required in order Slack to work. Next will see how to notify to Slack channel.
This it the last section – now go and create a notification class via artisan –
php artisan make:notification NotifyToSlackChannel.
Open up the
NotifyToSlackChannel class and do two things:
One, update the
via() method to accept only
slack in the array.
Two, add a new method called
toSlack($notification) and use
IlluminateNotificationsMessagesSlackMessage to send messages to Slack channel.
Both steps above in following gist:
So, you are ready to send messages to your Slack channel!
The fastest way to test your Slack notification is using tinker.
Here how I did:
Save it to
php artisan tinker tinker/notify-to-slack.php.
That’s it! Working great!
Some of user cases might need this type of notification:
- Helpdesk system – quick response from support team when get notify on new issues raised.
- E-commerce – placing new order, successful payment, out of stock items.
- Attendance system – you want know what time your employee come to your office (of course involve some other integration)
And I believe, it’s quite a lot of user cases can be implement, especially those involved with support, request, management.
Feel free to share your idea how we can implement more with Laravel Notification and Slack. 🙂
Happy coding guys!
4 thoughts on “Laravel: Notification to Slack”
I have problem with run php artisan config:cache..
please tell me possible solution for that…
Thanks in advance..
what’s the issue with the
php artisan config:cache? any error messages?
You can use the same process to integrate Laravel notifications through email as well. For this you will have to define toMail() in your notification file. Now use via() method to choose email as your notification method.
PHP Fatal error: Call to undefined method Closure::__set_state() in /var/www/live/vendor/config.php on line 56