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
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.

Visit Paragraph
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
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
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
Larafast: Laravel SaaS Starter Kit logo

Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes. Available with VILT and TALL stacks.

Larafast: Laravel SaaS Starter Kit
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

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

Rector

The latest

View all →
Anonymous Event Broadcasting in Laravel 11.5 image

Anonymous Event Broadcasting in Laravel 11.5

Read article
Microsoft Clarity Integration for Laravel image

Microsoft Clarity Integration for Laravel

Read article
Apply Dynamic Filters to Eloquent Models with the Filterable Package image

Apply Dynamic Filters to Eloquent Models with the Filterable Package

Read article
Property Hooks Get Closer to Becoming a Reality in PHP 8.4 image

Property Hooks Get Closer to Becoming a Reality in PHP 8.4

Read article
Asserting Exceptions in Laravel Tests image

Asserting Exceptions in Laravel Tests

Read article
Reversible Form Prompts and a New Exceptions Facade in Laravel 11.4 image

Reversible Form Prompts and a New Exceptions Facade in Laravel 11.4

Read article