The issue here is that the Notification Facade is not macroable. In Laravel, only classes that use the Macroable trait support the macro() method. The Illuminate\Support\Facades\Notification facade does not use this trait, which is why you're seeing the error:
Call to undefined method Illuminate\Notifications\Channels\MailChannel::macro()
This error occurs because the facade is forwarding the macro call to its underlying manager, which doesn't have the macro method either.
What Can You Do Instead?
If you want to encapsulate your Slack notification logic, you have a few options:
1. Create a Helper Function
You can define a global helper function (e.g., in app/helpers.php):
if (! function_exists('notify_slack')) {
function notify_slack($notifiable, $notification)
{
\Illuminate\Support\Facades\Notification::route('slack', $notifiable->routeNotificationForSlack())
->notify($notification);
}
}
Then use it like:
notify_slack($user, new MySlackNotification());
2. Create a Custom Service
You can create a service class to encapsulate this logic:
namespace App\Services;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Notification as NotificationFacade;
class SlackNotifier
{
public function send($notifiable, Notification $notification)
{
NotificationFacade::route('slack', $notifiable->routeNotificationForSlack())
->notify($notification);
}
}
Register it in the service container and inject/use it where needed.
3. Use a Trait
If you want to use this in multiple places, a trait might be helpful:
namespace App\Traits;
use Illuminate\Support\Facades\Notification;
trait SendsSlackNotifications
{
public function sendSlackNotification($notifiable, $notification)
{
Notification::route('slack', $notifiable->routeNotificationForSlack())
->notify($notification);
}
}
Then use the trait in your classes.
Summary:
The Notification facade is not macroable, so you can't add macros to it. Instead, use a helper, service, or trait to encapsulate your Slack notification logic and reduce duplication.