Customizing Slack Channel Notifications in Laravel 10

Customizing Slack Channel Notifications in Laravel 10
Customizing Slack Channel Notifications in Laravel

Working on my application, CareNote, I want to capture when a user decides to mark their account for deletion. I found Ralph J. Smit's blog entry on How to send messages from Laravel to Slack, but I want to conditional post to a specific channel based on the notification that is being sent, mainly to help keep my Slack channels clean. This is possible with a conditional with an early return.

Adding Logic to the User Model:

Let's start by adding some logic to the User.php model. Suppose you want to send different notifications when an account is deleted. You could do something like this:

public function routeNotificationForSlack(Notification $notification): mixed
    {
        if ($notification instanceof \App\Notifications\SlackNotification) {
            return '#custom-channel';
        }

        return '#default-channel';
    }

This code snippet checks if the notification being sent is an instance of SlackNotification. If it is, it uses a different slack channel.