laravel/slack-notification-channel
Official Laravel notification channel for sending notifications to Slack. Integrates with Laravel’s Notifications system, letting you route messages to Slack channels or users and customize payloads. See Laravel docs for configuration and usage.
Installation:
composer require laravel/slack-notification-channel
Publish the config (if needed):
php artisan vendor:publish --provider="Laravel\SlackNotificationChannel\SlackServiceProvider"
Configure Slack Webhook:
Add your Slack webhook URL to .env:
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/XXX/YYY/ZZZ
First Notification:
Create a notification class (e.g., app/Notifications/SlackAlert.php):
use Illuminate\Notifications\Notification;
use Laravel\SlackNotificationChannel\SlackMessage;
class SlackAlert extends Notification
{
public function via($notifiable)
{
return ['slack'];
}
public function toSlack($notifiable)
{
return (new SlackMessage)
->content('Hello from Laravel!')
->attachment(function ($attachment) {
$attachment->title('New Alert')
->text('Check this out!');
});
}
}
Send the Notification:
use App\Notifications\SlackAlert;
use App\Models\User; // or any notifiable model
$user = User::find(1);
$user->notify(new SlackAlert());
SlackMessage and SlackWebhookChannel classes in src/.config/services.php for Slack credentials and defaults.Use SlackMessage for simple text-based notifications:
return (new SlackMessage)
->content('Deployment failed on staging!')
->from('Laravel CI', ':robot_face:');
Leverage Slack’s attachment fields for structured data:
return (new SlackMessage)
->attachment(function ($attachment) {
$attachment->title('Order #12345')
->fields([
['title' => 'Customer', 'value' => 'John Doe', 'short' => true],
['title' => 'Amount', 'value' => '$99.99', 'short' => true],
])
->footer('Paid at ' . now()->format('H:i'));
});
Use Slack’s BlockKit for interactive elements (buttons, modals, selects):
return (new SlackMessage)
->blockKit([
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => 'Approve this payment?',
],
'accessory' => [
'type' => 'button',
'text' => [
'type' => 'plain_text',
'text' => 'Approve',
],
'action_id' => 'approve_payment',
],
]);
blockKit() method.Reply to existing Slack messages in threads:
return (new SlackMessage)
->content('Follow-up: Issue resolved!')
->threadTimestamp('1620000000.000123'); // Original message's timestamp
Skip notifications based on logic:
public function via($notifiable)
{
return $notifiable->shouldReceiveAlerts ? ['slack'] : [];
}
Override the webhook URL per notification:
public function toSlack($notifiable)
{
return (new SlackMessage)
->webhookUrl($notifiable->slack_webhook_url)
->content('Custom webhook used!');
}
Add user selects or buttons with callbacks:
return (new SlackMessage)
->blockKit([
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => 'Select an option:',
],
'accessory' => [
'type' => 'users_select',
'placeholder' => 'Choose a user...',
'action_id' => 'select_user',
],
]);
Use Laravel’s queue system to avoid blocking requests:
$user->notify(new SlackAlert())->onQueue('slack');
Configure Slack’s rate limits (1 per second by default). Use sleep() or queues to throttle:
if ($lastSentAt && now()->diffInSeconds($lastSentAt) < 1) {
sleep(1);
}
Wrap Slack calls in try-catch blocks to log failures:
try {
$user->notify(new SlackAlert());
} catch (\Exception $e) {
\Log::error('Slack notification failed', ['error' => $e->getMessage()]);
}
Use Laravel’s NotificationFake for unit tests:
use Illuminate\Support\Facades\Notification;
Notification::fake();
$user->notify(new SlackAlert());
Notification::assertSentTo($user, SlackAlert::class);
Use .env variables to switch between dev/staging/prod webhooks:
SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL:-http://localhost/slack-webhook}
Extend SlackWebhookChannel for reusable logic:
namespace App\Notifications\Channels;
use Laravel\SlackNotificationChannel\SlackWebhookChannel;
class CustomSlackChannel extends SlackWebhookChannel
{
public function send($notifiable, Notification $notification)
{
// Custom logic before sending
$result = parent::send($notifiable, $notification);
// Custom logic after sending
return $result;
}
}
Then register it in AppServiceProvider:
Notification::extend('custom_slack', function ($app) {
return new CustomSlackChannel();
});
Webhook URL Validation:
https. HTTP URLs will fail silently.if (!str_starts_with(config('services.slack.webhook_url'), 'https')) {
throw new \InvalidArgumentException('Slack webhook must use HTTPS.');
}
Attachment Size Limits:
->text() for large content or link to external resources.Thread Timestamps:
threadTimestamp values will create new threads instead of replies.1620000000.000123).BlockKit Validation:
Rate Limits:
Empty Routes:
via() or invalid channels may fail silently.public function via($notifiable)
{
return $notifiable->slack_channel ? ['slack'] : [];
}
Deprecated Methods:
->toSlack() directly. Prefer SlackMessage for consistency.SlackMessage builder methods.buildJsonPayload() to log the final payload:
public function buildJsonPayload($notifiable, $notification)
{
$payload
How can I help you explore Laravel packages today?