Laravel Subscribable Notifications
Published on by Paul Redmond
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:
Notification::send([$user1, $user2, $user3], new OrderShipped($order)); // or$notification = new OrderShipped($order); $user1->notify($notification);$user2->notify($notification);$user3->notify($notification);
Setting up notifications to use this package involves using the DispatchesToSubscribers
trait and a SubscribableNotification
interface:
use Asantibanez\LaravelSubscribableNotifications\Traits\ DispatchesToSubscribers; class SalesOrderApprovedNotification extends Notification implements SubscribableNotification{ use DispatchesToSubscribers; public static function subscribableNotificationType(): string { return 'sales-order.approved'; } // notification implementation here}
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.