Join 26,000+ Laravel Developers and join the free Laravel Newsletter
A Stand-Alone Laravel Slack Package
Laravel Packages / updated: June 18, 2018

A Stand-Alone Laravel Slack Package

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.

Laravel News Partners

Newsletter

Join the weekly newsletter and never miss out on new tips, tutorials, and more.