Laravel Subscribable Notifications

Packages

October 12th, 2021

laravel-subscribable-notifications-featured.png

Laravel Subscribable Notifications is a package by Andrés Santibáñez that allows you to subscribe your app's users to your app notifications and dispatch them without specifying the recipient:

This package allows you to subscribe your app Users to your app Notifications and dispatch them without specifying the recipient. The main goal of this package is to enable you to create "lists of recipients" for your Notification classes and dispatch notifications to your users easily.

At a high level, here's an example of how you can send a notification to multiple users using this package:

1Notification::send([$user1, $user2, $user3], new OrderShipped($order));
2 
3// or
4$notification = new OrderShipped($order);
5 
6$user1->notify($notification);
7$user2->notify($notification);
8$user3->notify($notification);

Setting up notifications to use this package involves using the DispatchesToSubscribers trait and a SubscribableNotification interface:

1use Asantibanez\LaravelSubscribableNotifications\Traits\ DispatchesToSubscribers;
2 
3class SalesOrderApprovedNotification extends Notification implements SubscribableNotification
4{
5 use DispatchesToSubscribers;
6 
7 public static function subscribableNotificationType(): string
8 {
9 return 'sales-order.approved';
10 }
11 
12 // notification implementation here
13}

This package also supports subscribing and unsubscribing notification subscriptions. Check the readme for full details on setting up and using this package. You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.