Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

A package to quickly send Slack alerts in a Laravel app

Original – by Freek Van der Herten – 2 minute read

Our team has released a small package called spatie/laravel-slack-alerts. It can be used to send a message to Slack quickly. In this blog post, I'd like to share why we've created this package and how you can use it.

Why we've created this package

At Spatie, we've created several applications where we as a team are interested in certain events that occur. For instance, we like to know whenever someone subscribes to Flare. There's a dedicated #flare-hooks channel in our Slack where we get these alerts.

Sending such "system" alerts via Laravel's otherwise excellent notification system is a bit cumbersome. You need to create a notifiable (or use the anonymous one) and create the notification itself.

You might think that the alternative could be to use one of the available Slack packages and manually send a request to Slack. That would work, but ideally, calling Slack should happen in a job so that our users don't see any errors in their web requests when Slack is down.

Send Slack alerts

Using our package, you can conveniently send a message to Slack. Once you've set up the Slack webhook in the config file, you can send an alert using this code:

SlackAlert::message('Hi there');

Behind the scenes, this code will dispatch a job to send that message to Slack. So, it's safe to use this code in a web request.

You can also use multiple channels:

SlackAlert::to('marketing')->message('We got a new subscriber.');

And that's all there is to this package.

In closing

We've tried to make the package as simple as possible. We could have added more features, such as message formatting, or support for other platforms besides Slack, but we opted to keep it as simple as it can be.

Take a look at the readme on GitHub to know more about spatie/laravel-slack-alerts. This package was created by my colleague Niels. Head over to our website to see a list of other packages our team created previously.

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.


What are your thoughts on "A package to quickly send Slack alerts in a Laravel app"?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.