A Stand-Alone Laravel Slack Package

Published on by

A Stand-Alone Laravel Slack Package image

The gpressutto5/laravel-slack composer package is a Slack notification library for Laravel 5.5 by Guilherme Pressutto:

Slack notification for Laravel as it should be. Easy, fast, simple and highly testable. Since it uses On-Demand Notifications, it requires Laravel 5.5 or higher.

You can install this dependency in your Laravel >= 5.5 application with the following:

composer require gpressutto5/laravel-slack

You can publish the configuration file with:

php artisan vendor:publish --provider="Pressutto\LaravelSlack\ServiceProvider"

You will also need to configure an incoming webhook integration for your Slack team.

Basic Usage

Here’s an example of the API interface for the provided Slack service that ships with this package:

// Send a message to a channel
\Slack::to('#finance')->send('Hey, finance channel! A new order was created just now!');
 
// Send message to users
\Slack::to(['@zoe', '@amy', '@mia'])
->send('Here is an example message sent to multiple users');

You can mix it up sending a message to both users and channels at the same time:

\Slack::to('#support', '@bill', '#dev')
->send(':thinking_face:');

You can even send to a user by passing an Eloquent model that has a slack_channel property, or you can provide an accessor method:

class User extends Model
{
public function getSlackChannelAttribute(): string
{
return $this->attributes['my_custom_slack_channel_column'];
}
}
\Slack::to(User::where('verified', true))
->send('Sending message to all verified users!');

Here’s a reference to all the configuration values at the time of writing:

<?php
 
return [
'slack_webhook_url' => env('SLACK_WEBHOOK_URL', ''),
'default_channel' => '#general',
'application_name' => env('APP_NAME', null),
'application_image' => null,
];

Testing

You can easily mock the Slack service by calling Slack::fake() and messages will not be sent to Slack but will be stored in an array. The package ships with some testing helper methods to aid your tests, for example:

Slack::assertSent(function (SlackMessage $message) {
return $message->content === 'fake';
});
 
Slack::assertSentCount(3);

Learn More

Check out the official GitHub repository and the readme to learn more about this package, including how to write tests for the functionality provided by this package.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Tinkerwell

Version 4 of Tinkerwell is available now. Get the most popular PHP scratchpad with all its new features and simplify your development workflow today.

Visit Tinkerwell
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
LoadForge logo

LoadForge

Easy, affordable load testing and stress tests for websites, APIs and databases.

LoadForge
Paragraph logo

Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Paragraph
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
DocuWriter.ai logo

DocuWriter.ai

Save hours of manually writing Code Documentation, Comments & DocBlocks, Test suites and Refactoring.

DocuWriter.ai
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
Non-backed Enums in Database Queries and a withSchedule() bootstrap method in Laravel 11.1 image

Non-backed Enums in Database Queries and a withSchedule() bootstrap method in Laravel 11.1

Read article
Laravel Pint --bail Flag image

Laravel Pint --bail Flag

Read article
Laravel Herd for Windows is now released! image

Laravel Herd for Windows is now released!

Read article
The Laravel Worldwide Meetup is Today image

The Laravel Worldwide Meetup is Today

Read article
Cache Routes with Cloudflare in Laravel image

Cache Routes with Cloudflare in Laravel

Read article
Learn how to manage timezones in your Laravel Apps image

Learn how to manage timezones in your Laravel Apps

Read article