Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MaxEckel's avatar

Sending Notification to multiple Slack Webhooks

Hey Guys,

is there a way to send a notification to multiple Webhook URLs? Sending to only one Channel is pretty easy, but what if i want to be able to define multiple Channels?

Since "routeNotificationForSlack" expects a String as Return parameter, passing an array/collection doesn't work.

Do you know a way to do this?

0 likes
8 replies
hotsaucejake's avatar

I tried setting up a dummy model that is Notifiable and has a different routeNotificationForSlack() but it gave me a PDO error because it didn't have an associated table.

So I made an empty table.

Now the notification "processes" with no error, but it doesn't show up on the separate webhook... although the model has a different webhook.

It seems my "solution" doesn't work because it uses the first webhook of the notifiable. If you have another notifiable that's called, then it gets "processed" but not actually because it never fires. It's the first webhook that's called.

hotsaucejake's avatar

It'd be nice if someone had a clever way to make the same messages push to separate webhooks.

cliftonscott's avatar

I think my solution is simple.

Add to Users.php

public $webhook;

public function routeNotificationForSlack(  ) {
            if($this->webhook){
                return config('slack.channels.'.$this->webhook);
            }
    }

public function slackChannel($channel){
    $this->webhook = $channel;
    return $this;
}

config/slack.php

return [
    'channels' => [
        'name-of-channel' => 'url-of-channel',
        'name-of-next-channel' => 'url-of-next-channel'
    ]
];

To send notification to slack channel

$user->slackChannel('name-of-channel')->notify(new Notification());
7 likes
andrewc's avatar

Excellent suggestion! Weird thing is in Laravel 5.3, $this->webhook is null in routeNotificationForSlack

UPDATE: Managed to make this work by creating a empty class that uses the Notifiable trait. I then pass the Channel through the construct to then call:

Notification::send(new SlackUpdates('channel_name'), new Notify($data));
1 like
OK200Paul's avatar

Hey everyone, I thought I'd post back in here as I found a viable workaround. @cliftonscott there's a slight issue with your solution, as the User class is sometimes called upon after the notification process is in place, causing $webhook to be intermittently null. So it worked for me for a while, but began failing/defaulting to null when we got to more sophisticated implementations.

Here's a workaround that seems to work for me, I'm quite impressed at how easy it works in Laravel. I'm using PHP8 syntax here but I think it'll work in PHP7 with a bit of fiddling:

In the notification, pop in a nullable constructor argument for an alternative webhook URL:

class SlackNotificationInLaravelApp extends Notification implements ShouldQueue
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(public ?string $webhookUrl = null)
    {
        //
    }

.. then, in the User model update the routeNotificationForSlack function to look like this:

    /**
     * Route notifications for the Slack channel.
     *
     * @param Notification $notification
     *
     * @return string|null
     */
    public function routeNotificationForSlack($notification): string|null
    {
        // If the incoming notification has an alternative webhookUrl set, use, it - otherwise use default
        return $notification->webhookUrl ?? 'https://hooks.slack.com/services/...'; 
    }

.. then, when notifying a user, pop the alternative webhook URL in as an argument:

// Notify the user but send it to a different channel
$user->notify(new SlackNotificationInLaravelApp(webhookUrl: 'https://hooks.slack.com/services/...'));

// Notify the user but send it to the default channel - simply leave the argument out 👍
$user->notify(new SlackNotificationInLaravelApp());

Hope this helps!

fayce's avatar

the ->to('#channel') method only works with the legacy A0F7XDUAZ-incoming-webhooks application available in the slack marketplace

IAA's avatar

This is an old thread but still hasn't been answsered per the OPs requirements. I ran into a similar issue with the slack driver this morning, and the fix was trivial.

A user can create many notification channels, and they can create multiple instances of a single channel if they wanted.

  • Create a new Slack Webhook Channel that extends the Laravel one and overwrites the ->send() function
<?php

namespace App\Notifications\Channels;

use Illuminate\Notifications\Notification;

class SlackWebhookChannel extends \Illuminate\Notifications\Channels\SlackWebhookChannel
{
    public function send($notifiable, Notification $notification): mixed
    {
        if (!$urls = $notifiable->routeNotificationFor('slack', $notification)) {
            return null;
        }
        return $urls->each(
            function ($url) use ($notifiable, $notification) {
                return $this->http->post($url, $this->buildJsonPayload($notification->toSlack($notifiable)));
            });
    }
}

This change simply assumes a collection is returned from your notifiable instance instead of a string.

  • Overwrite the slack driver in AppServiceProvider->register()
use App\Notifications\Channels\SlackWebhookChannel;

class AppServiceProvider extends ServiceProvider
{
		public function register(): void
		{
				Notification::resolved(function (ChannelManager $service) {
            		$service->extend('slack', function ($app) {
                		return new SlackWebhookChannel($app->make(HttpClient::class));
            		});
        		});
		}
}

This tells Laravel that when you want the slack driver, to use the newly created extension.

  • Edit your notifiable instance to return a collection, not a string. In my case, I am using the Team model from Jetstream, with a notificationChannels() relationship so the Team can create as many channels as needed.

class Team extends JetstreamTeam
    public function routeNotificationFor($driver, $notification = null): Collection
    {
        return $this->notificationChannels()->where('type', $driver)->pluck('route');
    }
}

Use the slack driver normally as per the docs - but with the ability to add as many slack webhooks as you need!

Please or to participate in this conversation.