Download the PHP package codinglabsau/laravel-notification-subscriptions without Composer

On this page you can find all versions of the php package codinglabsau/laravel-notification-subscriptions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-notification-subscriptions

Laravel Notification Subscriptions

Latest Version on Packagist Test Total Downloads

A Laravel package for managing user notification preferences across multiple channels. Let your users control how they receive notifications (email, in-app, push, Slack) while you maintain sensible defaults and rate limiting.

Features

Installation

1. Install via Composer

2. Publish and run migrations

3. Publish configuration (optional)

4. Create your channel enum

Create an enum that implements SubscribableChannel to define your notification channels:

5. Add trait to your User model

6. Publish and configure the service provider

Then register your subscribable notifications in app/Providers/NotificationSubscriptionsServiceProvider.php:

Don't forget to add this service provider to your bootstrap/providers.php:

Basic Usage

Creating a Subscribable Notification

Transform any Laravel notification into a subscribable notification by implementing the SubscribableNotification interface and using the DispatchesNotifications trait:

Dispatching Notifications

Use the static sendToSubscribers() method instead of Laravel's standard notification sending:

Transactional vs Subscribable Notifications

Use Case Method Behavior
Transactional (password reset, order confirmation) Standard Laravel $user->notify() Always sends, no filtering
Subscribable (messages, updates, marketing) Notification::sendToSubscribers() Respects user preferences

How It Works

When a notification is dispatched:

  1. Subscriber lookup - The subscribers() method determines who should receive the notification
  2. Channel filtering - For each subscriber, the package checks their preferences:
    • If they have a stored preference for this notification type, only enabled channels are used
    • If no preference exists, channels with defaultOn() === true are used
  3. Rate limiting - If a channel has rate limiting enabled and the notification has a subject, duplicate notifications are throttled
  4. Delivery - The notification is sent only to the appropriate channels

Channel Enum Reference

Your channel enum must implement SubscribableChannel with these methods:

Method Return Type Description
driver() string Laravel notification channel driver (e.g., 'database', 'mail', OneSignalChannel::class)
label() string Human-readable label for UI (e.g., 'Email', 'Push Notifications')
isEnabled() bool Whether this channel is currently available
defaultOn() bool Whether new users have this channel enabled by default
hasRateLimiting() bool Whether rate limiting applies to this channel
rateLimitDuration() int Rate limit duration in seconds

Example: Advanced Channel Configuration

Rate Limiting

Rate limiting prevents notification spam when the same notification could be triggered multiple times in quick succession.

How Rate Limiting Works

Rate limits are applied per combination of:

Subject Method

For rate limiting to work, your notification must return a subject:

If subject() returns null, rate limiting is skipped for that notification.

Mandatory Channels

Some notifications must always be sent via certain channels regardless of user preferences. For example, a support ticket reply might always need an email notification, even if the user has opted out of email for other notifications.

Defining Mandatory Channels

Override mandatoryChannels() in your notification class to specify channels that cannot be unsubscribed from:

By default, mandatoryChannels() returns an empty array, meaning all channels are optional.

How Mandatory Channels Work

Mandatory channels are enforced at three levels:

  1. shouldSend() defense-in-depth — When dispatching a notification, mandatory channels always return true in shouldSend(), even if the user's subscription record excludes them. This ensures delivery even with stale subscription data.

  2. Validation re-injection — The ValidatesNotificationPreferences trait automatically re-injects mandatory channels into form requests during prepareForValidation(), so they can never be removed by user input.

  3. NotificationPreferences DTO — The getNotificationPreferences() method populates a mandatory property on the DTO, mapping each notification type to its mandatory channel values. This allows your UI to render mandatory channels as disabled/locked checkboxes.

Using Mandatory Data in the UI

The NotificationPreferences DTO includes a mandatory property:

Use this in your frontend to disable checkboxes for mandatory channels:

Building a Settings UI

The package provides a simple API for building notification preference UIs. The HasNotificationSubscriptions trait adds two methods to your User model:

Controller Setup

The ValidatesNotificationPreferences trait:

The NotificationPreferences DTO

The getNotificationPreferences() method returns a NotificationPreferences object with three properties:

Example Blade Template

Inertia/Vue Example

Advanced Usage

Custom Notification Labels

Add metadata methods to your notifications for richer UIs:

Before Send Hook

Execute code before any notification is sent:

Custom Subscription Model

Extend the base model if you need additional functionality:

Database Schema

The package creates a notification_subscriptions table:

Column Type Description
id bigint Primary key
user_id bigint Foreign key to users table
type string Notification type identifier
channels json Array of enabled channel names
created_at timestamp Creation timestamp
updated_at timestamp Last update timestamp

A unique constraint ensures one subscription record per user/type combination.

Testing

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-notification-subscriptions with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
illuminate/contracts Version ^12.0|^13.0
spatie/laravel-package-tools Version ^1.9.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package codinglabsau/laravel-notification-subscriptions contains the following files

Loading the files please wait ...