How to Configure Slack Alerts for Laravel Exceptions
Learn how to set up real-time Slack notifications for Laravel exceptions. Get instant alerts when errors occur in production so you can respond faster and keep your users happy.
When your Laravel application throws an exception in production, every second counts. The faster you know about it, the faster you can fix it. In this guide, we'll walk through setting up Slack alerts for Laravel exceptions so you get instant notifications the moment something goes wrong.
Why Slack Alerts for Exceptions?
Email notifications are fine, but they're easy to miss. Slack, on the other hand, is where most development teams already spend their time. By routing exception alerts to Slack, you get:
- Instant visibility - Alerts appear in a channel your team monitors
- Context at a glance - See the error message, file, and line number without opening another app
- Team awareness - Everyone sees the alert, reducing duplicate investigations
- Mobile notifications - Get alerted even when you're away from your desk
Prerequisites
Before we begin, make sure you have:
- A Laravel 10+ application
- A Slack workspace with permission to create webhooks
- Composer installed for package management
Step 1: Create a Slack Webhook
First, you'll need to create an incoming webhook in Slack:
- Go to api.slack.com/apps and click "Create New App"
- Choose "From scratch" and give your app a name like "Laravel Alerts"
- Select your workspace and click "Create App"
- Under "Features", click "Incoming Webhooks"
- Toggle "Activate Incoming Webhooks" to On
- Click "Add New Webhook to Workspace"
- Select the channel where you want alerts to appear
- Copy the webhook URL - you'll need this shortly
Step 2: Install the Slack Notification Channel
Laravel's notification system makes it easy to send messages to Slack. Install the Slack notification channel:
composer require laravel/slack-notification-channel
Step 3: Add the Webhook to Your Environment
Add your Slack webhook URL to your .env file:
SLACK_ALERT_WEBHOOK=https://hooks.slack.com/services/YOUR/WEBHOOK/URL
Then add the config value in config/services.php:
'slack' => [
'alerts' => [
'webhook_url' => env('SLACK_ALERT_WEBHOOK'),
],
],
Step 4: Create an Exception Notification
Create a notification class that will format your exception alerts nicely:
php artisan make:notification ExceptionOccurred
Update the generated file at app/Notifications/ExceptionOccurred.php:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Slack\BlockKit\Blocks\SectionBlock;
use Illuminate\Notifications\Slack\SlackMessage;
use Throwable;
class ExceptionOccurred extends Notification
{
use Queueable;
public function __construct(
public Throwable $exception
) {}
public function via(object $notifiable): array
{
return ['slack'];
}
public function toSlack(object $notifiable): SlackMessage
{
$exception = $this->exception;
return (new SlackMessage)
->text('🚨 Exception in ' . config('app.name'))
->headerBlock('Exception Alert')
->sectionBlock(function (SectionBlock $block) use ($exception) {
$block->text(
"*{$exception->getMessage()}*\n\n" .
"File: `{$exception->getFile()}`\n" .
"Line: {$exception->getLine()}\n" .
"Environment: " . config('app.env')
);
})
->dividerBlock()
->contextBlock(function ($block) {
$block->text('Timestamp: ' . now()->toDateTimeString());
});
}
}
Step 5: Create a Notifiable for Slack
Since we're not sending to a user, we need a simple notifiable class:
php artisan make:class Notifications/SlackChannel
Update app/Notifications/SlackChannel.php:
<?php
namespace App\Notifications;
use Illuminate\Notifications\Notifiable;
class SlackChannel
{
use Notifiable;
public function routeNotificationForSlack(): string
{
return config('services.slack.alerts.webhook_url');
}
}
Step 6: Hook Into Laravel's Exception Handler
In Laravel 11+, exception handling is configured in bootstrap/app.php. Add the reporting logic:
->withExceptions(function (Exceptions $exceptions) {
$exceptions->report(function (Throwable $e) {
if (app()->environment('production') && config('services.slack.alerts.webhook_url')) {
$channel = new \App\Notifications\SlackChannel;
$channel->notify(new \App\Notifications\ExceptionOccurred($e));
}
});
})
This ensures alerts only fire in production and only when the webhook is configured.
Step 7: Test Your Setup
Create a simple test route to verify everything works:
Route::get('/test-exception', function () {
throw new \Exception('Test exception for Slack alert!');
});
Visit this route in your browser, and you should see a nicely formatted alert appear in your Slack channel within seconds.
Best Practices
Don't Alert on Everything
Some exceptions are expected (like 404s or validation errors). Filter these out to avoid alert fatigue:
$exceptions->report(function (Throwable $e) {
// Skip common non-critical exceptions
if ($e instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
return false;
}
if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
return false;
}
// Send to Slack
if (app()->environment('production') && config('services.slack.alerts.webhook_url')) {
$channel = new \App\Notifications\SlackChannel;
$channel->notify(new \App\Notifications\ExceptionOccurred($e));
}
});
Add Rate Limiting
If the same exception fires repeatedly, you don't want to flood your Slack channel. Consider using Laravel's cache to rate limit alerts:
$exceptions->report(function (Throwable $e) {
$cacheKey = 'exception_alert_' . md5($e->getMessage() . $e->getFile());
if (Cache::has($cacheKey)) {
return false; // Already alerted recently
}
Cache::put($cacheKey, true, now()->addMinutes(5));
// Send alert...
});
Include Request Context
For debugging, it helps to know what request triggered the exception. Enhance your notification:
public function toSlack(object $notifiable): SlackMessage
{
$exception = $this->exception;
$request = request();
return (new SlackMessage)
->text('🚨 Exception in ' . config('app.name'))
->headerBlock('Exception Alert')
->sectionBlock(function (SectionBlock $block) use ($exception, $request) {
$block->text(
"*{$exception->getMessage()}*\n\n" .
"URL: `{$request->fullUrl()}`\n" .
"Method: {$request->method()}\n" .
"File: `{$exception->getFile()}`\n" .
"Line: {$exception->getLine()}"
);
});
}
Taking It Further
Slack alerts are a great first step, but for comprehensive error tracking, you'll want a dedicated solution. Tools like 401 Clicks give you:
- Full stack traces with local variables
- Error grouping to identify patterns
- Historical trends and error frequency
- Integration with your existing Slack workflow
The setup we've covered here will get you immediate visibility into production errors. Combined with a proper error tracking service, you'll never be surprised by an exception again.
Conclusion
Setting up Slack alerts for Laravel exceptions takes about 15 minutes but can save you hours of debugging time. By getting instant notifications when errors occur, you can:
- Respond to issues before users report them
- Track down bugs while the context is fresh
- Build confidence in your production deployments
Start with the basic setup, then customize it to fit your team's workflow. Your future self will thank you the next time a critical error pops up at 2 AM.
Admin
Published on January 7, 2026