Menu

1 / 2

Per-user notification preferences in Laravel without rewriting via()

DEV Community: php·Sebastian Cabarcas·5 months ago
#GKVSTshq
#dev#user#orders#channels#laravel
Reading 0:00
15s threshold

Every SaaS ends up building the same thing: a notification settings screen where users opt in/out of email, push, Slack, etc., per category. The logic always leaks into via() arrays and policy classes scattered across the app.

I extracted this into a package: Laravel Notify Matrix.

The problem

// Before — what every Laravel app eventually does: public function via ( $notifiable ): array { $channels = [ 'database' ];

if ( $notifiable -> wantsMail ( 'orders' )) { $channels [] = 'mail' ; }

if ( $notifiable -> wantsSlack ( 'orders' ) && config ( 'app.slack_enabled' )) { $channels [] = 'slack' ; }

return $channels ; }

Enter fullscreen mode

Exit fullscreen mode

The package

// User model use Scabarcas\LaravelNotifyMatrix\Concerns\HasNotificationPreferences ;

class User extends Authenticatable { use HasNotificationPreferences ; }

// Notification class use Scabarcas\LaravelNotifyMatrix\Attributes\NotificationGroup ;

#[NotificationGroup('orders')] class OrderShipped extends Notification { public function…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.