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!