PHPackages                             liran-co/laravel-notification-subscriptions - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. liran-co/laravel-notification-subscriptions

ActiveLibrary[Mail &amp; Notifications](/categories/mail)

liran-co/laravel-notification-subscriptions
===========================================

Notification subscription management.

v1.8(2y ago)128239.2k—7.6%26[3 PRs](https://github.com/liran-co/laravel-notification-subscriptions/pulls)1MITPHPPHP ^7.2.5|^8.0|^8.1CI failing

Since May 16Pushed 2y ago7 watchersCompare

[ Source](https://github.com/liran-co/laravel-notification-subscriptions)[ Packagist](https://packagist.org/packages/liran-co/laravel-notification-subscriptions)[ Docs](https://github.com/liran-co/laravel-notification-subscriptions)[ RSS](/packages/liran-co-laravel-notification-subscriptions/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (13)Used By (1)

Laravel Notification Subscriptions
==================================

[](#laravel-notification-subscriptions)

Laravel Notification Subscriptions is a package that hooks directly into Laravel's existing [notification system](https://laravel.com/docs/master/notifications) and adds functionality to manage user subscriptions to your app's notifications and suppress them automatically when they shouldn't be sent. You can subscribe and unsubscribe users to specific notification channels, create opt-in notifications, and scope your subscriptions by another model.

[![Latest Stable Version](https://camo.githubusercontent.com/54f16faa41b87167b5bfd86c924bd3404844d27248cda6ad58b60074aabfa1a0/68747470733a2f2f706f7365722e707567782e6f72672f6c6972616e2d636f2f6c61726176656c2d6e6f74696669636174696f6e2d737562736372697074696f6e732f762f737461626c65)](https://packagist.org/packages/liran-co/laravel-notification-subscriptions) [![Total Downloads](https://camo.githubusercontent.com/0d631a0d5adcc0e5a2e1696acafe4b4a6c4122ebef6d857e8be58b0d9c45bc4a/68747470733a2f2f706f7365722e707567782e6f72672f6c6972616e2d636f2f6c61726176656c2d6e6f74696669636174696f6e2d737562736372697074696f6e732f646f776e6c6f616473)](https://packagist.org/packages/liran-co/laravel-notification-subscriptions) [![License](https://camo.githubusercontent.com/e3990507f2dd97712e7e8d524dad59479ec2d509937470e5113fbf4aaae4c29c/68747470733a2f2f706f7365722e707567782e6f72672f6c6972616e2d636f2f6c61726176656c2d6e6f74696669636174696f6e2d737562736372697074696f6e732f6c6963656e7365)](https://packagist.org/packages/liran-co/laravel-notification-subscriptions)

Installation
------------

[](#installation)

To get started, install the `liran-co/laravel-notification-subscriptions` package:

```
composer require liran-co/laravel-notification-subscriptions
```

Run the migration to create the `notification_subscriptions` table:

```
php artisan migrate
```

Optionally publish the configuration file by running and selecting the appropriate provider option:

```
php artisan vendor:publish
```

Basic usage
-----------

[](#basic-usage)

This package uses a [Listener](https://laravel.com/docs/master/events) to listen for any notifications that get sent in your application. When a notification gets triggered, the package checks to see if the notification should actually be sent according to the user's subscriptions. If not, the notification is suppressed.

### Getting started

[](#getting-started)

This package assumes you've already setup Laravel's notification system. If you haven't [read the docs](https://laravel.com/docs/master/notifications) to get started.

Add the `HasNotificationSubscriptions ` trait to your `User` model:

```
use Illuminate\Database\Eloquent\Model;
use LiranCo\NotificationSubscriptions\Traits\HasNotificationSubscriptions;

class User extends Model
{
    use HasNotificationSubscriptions;

    // ...
}
```

### Unsubscribing

[](#unsubscribing)

To unsubscribe a user from a specific `Notification`, pass the class name of that notification to the `unsubscribe` function.

```
use App\Notifications\InvoicePaid;

$user->unsubscribe(InvoicePaid::class); //You can also pass a string, but this is the preferred method.
```

The above will unsubscribe the user from all channels. You can unsubscribe a user from a specific channel by passing the channel name as the second parameter:

```
use App\Notifications\InvoicePaid;

$user->unsubscribe(InvoicePaid::class, 'mail');
```

Now, whenever an `InvoicePaid` notification is sent, the package will automatically detect that the user has unsubscribed and suppress the notification. For example:

```
use App\Notifications\InvoicePaid;

$user->notify(new InvoicePaid($invoice)); //This won't get sent.
```

### Opt-in notifications

[](#opt-in-notifications)

By default, all notifications will be sent if no subscribe/unsubscribe record is found. This means you don't need to explicitly **subscribe** a user to a notification, you only need to **unsubscribe** them.

In some cases, however, you'd like to create opt-in notifications. To do so, modify your notification class and add a function called `getOptInSubscriptions`:

```
