PHPackages                             overtrue/laravel-subscribe - 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. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. overtrue/laravel-subscribe

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

overtrue/laravel-subscribe
==========================

User Subscribe/Unsubscribe features for Laravel Application.

4.4.1(11mo ago)18349.6k↓23.3%15MITPHPCI failing

Since Apr 9Pushed 1mo ago4 watchersCompare

[ Source](https://github.com/overtrue/laravel-subscribe)[ Packagist](https://packagist.org/packages/overtrue/laravel-subscribe)[ GitHub Sponsors](https://github.com/overtrue)[ RSS](/packages/overtrue-laravel-subscribe/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (6)Versions (15)Used By (0)

Laravel Subscribe
-----------------

[](#laravel-subscribe)

📧 User subscribe/unsubscribe feature for Laravel Application.

[![CI](https://github.com/overtrue/laravel-subscribe/workflows/CI/badge.svg)](https://github.com/overtrue/laravel-subscribe/workflows/CI/badge.svg)

[![Sponsor me](https://github.com/overtrue/overtrue/raw/master/sponsor-me-button-s.svg?raw=true)](https://github.com/sponsors/overtrue)

Installing
----------

[](#installing)

```
$ composer require overtrue/laravel-subscribe -vvv
```

### Configuration

[](#configuration)

This step is optional

```
$ php artisan vendor:publish --provider="Overtrue\\LaravelSubscribe\\SubscribeServiceProvider" --tag=config
```

### Migrations

[](#migrations)

**You need to publish the migration files for use the package:**

```
$ php artisan vendor:publish --provider="Overtrue\\LaravelSubscribe\\SubscribeServiceProvider" --tag=migrations
```

Usage
-----

[](#usage)

### Traits

[](#traits)

#### `Overtrue\LaravelSubscribe\Traits\Subscriber`

[](#overtruelaravelsubscribetraitssubscriber)

```
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Overtrue\LaravelSubscribe\Traits\Subscriber;

class User extends Authenticatable
{
    use Subscriber;

}
```

#### `Overtrue\LaravelSubscribe\Traits\Subscribable`

[](#overtruelaravelsubscribetraitssubscribable)

```
use Illuminate\Database\Eloquent\Model;
use Overtrue\LaravelSubscribe\Traits\Subscribable;

class Post extends Model
{
    use Subscribable;

}
```

### API

[](#api)

```
$user = User::find(1);
$post = Post::find(2);

$user->subscribe($post);
$user->unsubscribe($post);
$user->toggleSubscribe($post);

$user->hasSubscribed($post);
$post->isSubscribedBy($user);
```

##### Get object subscribers:

[](#get-object-subscribers)

```
foreach($post->subscribers as $user) {
    // echo $user->name;
}
```

##### Aggregations

[](#aggregations)

```
// all
$user->subscriptions()->count();

// with type
$user->subscriptions()->withType(Post::class)->count();

// subscribers count
$post->subscribers()->count();
```

List with `*_count` attribute:

```
$users = User::withCount('subscriptions')->get();

foreach($users as $user) {
    echo $user->subscriptions_count;
}
```

### Filter subscribables

[](#filter-subscribables)

```
$posts = Post::hasSubscribers($user)->get();
$posts = Post::hasSubscribers($user->id)->get();
$posts = Post::hasSubscribers([$user1, $user2])->get();
$posts = Post::hasSubscribers([$user1->id, $user2->id])->get();

// or
$posts = Post::subscribedBy($user)->get();
$posts = Post::subscribedBy($user->id)->get();
$posts = Post::subscribedBy([$user1, $user2])->get();
$posts = Post::subscribedBy([$user1->id, $user2->id])->get();
```

### Order by subscribers count

[](#order-by-subscribers-count)

You can query subscribable model order by subscribers count with following methods:

- `orderBySubscribersCountDesc()`
- `orderBySubscribersCountAsc()`
- `orderBySubscribersCount(string $direction = 'desc')`

example:

```
$posts = Post::orderBySubscribersCountDesc()->get();
$mostPopularPost = Post::orderBySubscribersCountDesc()->first();
```

### N+1 issue

[](#n1-issue)

To avoid the N+1 issue, you can use eager loading to reduce this operation to just 2 queries. When querying, you may specify which relationships should be eager loaded using the `with` method:

```
// Subscriber
$users = App\User::with('subscriptions')->get();

foreach($users as $user) {
    $user->hasSubscribed($post);
}

// Subscribable
$posts = App\Post::with('subscriptions')->get();
// or
$posts = App\Post::with('subscribers')->get();

foreach($posts as $post) {
    $post->isSubscribedBy($user);
}
```

### Attach the subscription status to subscribable collection

[](#attach-the-subscription-status-to-subscribable-collection)

You can use `Subscriber::attachSubscriptionStatus(Collection $subscribeables)` to attach the user subscription status, it will set `has_subscribed` attribute to each model of `$subscribables`:

#### For model

[](#for-model)

```
$user1 = User::find(1);

$user->attachSubscriptionStatus($user1);

// result
[
    "id" => 1
    "name" => "user1"
    "private" => false
    "created_at" => "2021-06-07T15:06:47.000000Z"
    "updated_at" => "2021-06-07T15:06:47.000000Z"
    "has_subscribed" => true
  ]
```

#### For `Collection | Paginator | LengthAwarePaginator | array`:

[](#for-collection--paginator--lengthawarepaginator--array)

```
$user = auth()->user();

$posts = Post::oldest('id')->get();

$posts = $user->attachSubscriptionStatus($posts);

$posts = $posts->toArray();

// result
[
  [
    "id" => 1
    "title" => "title 1"
    "created_at" => "2021-06-07T15:06:47.000000Z"
    "updated_at" => "2021-06-07T15:06:47.000000Z"
    "has_subscribed" => true
  ],
  [
    "id" => 2
    "title" => "title 2"
    "created_at" => "2021-06-07T15:06:47.000000Z"
    "updated_at" => "2021-06-07T15:06:47.000000Z"
    "has_subscribed" => true
  ],
  [
    "id" => 3
    "title" => "title 3"
    "created_at" => "2021-06-07T15:06:47.000000Z"
    "updated_at" => "2021-06-07T15:06:47.000000Z"
    "has_subscribed" => false
  ],
  [
    "id" => 4
    "title" => "title 4"
    "created_at" => "2021-06-07T15:06:47.000000Z"
    "updated_at" => "2021-06-07T15:06:47.000000Z"
    "has_subscribed" => false
  ],
]
```

#### For pagination

[](#for-pagination)

```
$posts = Post::paginate(20);

$user->attachSubscriptionStatus($posts);
```

### Events

[](#events)

**Event****Description**`Overtrue\LaravelSubscribe\Events\Subscribed`Triggered when the relationship is created.`Overtrue\LaravelSubscribe\Events\Unsubscribed`Triggered when the relationship is deleted.Related packages
----------------

[](#related-packages)

- Follow: [overtrue/laravel-follow](https://github.com/overtrue/laravel-follow)
- Like: [overtrue/laravel-like](https://github.com/overtrue/laravel-like)
- Favorite: [overtrue/laravel-favorite](https://github.com/overtrue/laravel-favorite)
- Subscribe: [overtrue/laravel-subscribe](https://github.com/overtrue/laravel-subscribe)
- Vote: [overtrue/laravel-vote](https://github.com/overtrue/laravel-vote)
- Bookmark: overtrue/laravel-bookmark (working in progress)

❤️ Sponsor me
-------------

[](#heart-sponsor-me)

[![Sponsor me](https://github.com/overtrue/overtrue/raw/master/sponsor-me.svg?raw=true)](https://github.com/sponsors/overtrue)

如果你喜欢我的项目并想支持它，[点击这里 ❤️](https://github.com/sponsors/overtrue)

Project supported by JetBrains
------------------------------

[](#project-supported-by-jetbrains)

Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.

[![](https://camo.githubusercontent.com/3cf726e7cdadba47755b7f7ea4227945a92a2fa48aadf4a2573140ec6501c989/68747470733a2f2f7265736f75726365732e6a6574627261696e732e636f6d2f73746f726167652f70726f64756374732f636f6d70616e792f6272616e642f6c6f676f732f6a625f6265616d2e737667)](https://www.jetbrains.com/?from=https://github.com/overtrue)

Contributing
------------

[](#contributing)

You can contribute in one of three ways:

1. File bug reports using the [issue tracker](https://github.com/overtrue/laravel-subscribes/issues).
2. Answer questions or fix bugs on the [issue tracker](https://github.com/overtrue/laravel-subscribes/issues).
3. Contribute new features or update the wiki.

*The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable.*

PHP 扩展包开发
---------

[](#php-扩展包开发)

> 想知道如何从零开始构建 PHP 扩展包？
>
> 请关注我的实战课程，我会在此课程中分享一些扩展开发经验 —— [《PHP 扩展包实战教程 - 从入门到发布》](https://learnku.com/courses/creating-package)

License
-------

[](#license)

MIT

###  Health Score

54

—

FairBetter than 96% of packages

Maintenance72

Regular maintenance activity

Popularity47

Moderate usage in the ecosystem

Community21

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 75% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~148 days

Recently: every ~173 days

Total

14

Last Release

348d ago

Major Versions

1.1.0 → 2.0.02020-12-02

2.0.0 → 3.0.02021-06-09

2.x-dev → 3.1.02021-11-04

3.1.0 → 4.0.02022-02-10

### Community

Maintainers

![](https://www.gravatar.com/avatar/c507e57eab402e81335012357b7d7df6c5cafda3073adcc94b475037127d263f?d=identicon)[overtrue](/maintainers/overtrue)

---

Top Contributors

[![overtrue](https://avatars.githubusercontent.com/u/1472352?v=4)](https://github.com/overtrue "overtrue (36 commits)")[![d8vjork](https://avatars.githubusercontent.com/u/2331052?v=4)](https://github.com/d8vjork "d8vjork (3 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (3 commits)")[![s950329](https://avatars.githubusercontent.com/u/13188254?v=4)](https://github.com/s950329 "s950329 (1 commits)")[![x-adam](https://avatars.githubusercontent.com/u/60411758?v=4)](https://github.com/x-adam "x-adam (1 commits)")[![9x3l6](https://avatars.githubusercontent.com/u/113054034?v=4)](https://github.com/9x3l6 "9x3l6 (1 commits)")[![X-Adam](https://avatars.githubusercontent.com/u/60411758?v=4)](https://github.com/X-Adam "X-Adam (1 commits)")[![astro2049](https://avatars.githubusercontent.com/u/45759373?v=4)](https://github.com/astro2049 "astro2049 (1 commits)")[![idaraty](https://avatars.githubusercontent.com/u/65696547?v=4)](https://github.com/idaraty "idaraty (1 commits)")

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/overtrue-laravel-subscribe/health.svg)

```
[![Health](https://phpackages.com/badges/overtrue-laravel-subscribe/health.svg)](https://phpackages.com/packages/overtrue-laravel-subscribe)
```

###  Alternatives

[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135224.7k7](/packages/statamic-rad-pack-runway)[jeremy379/laravel-openid-connect

OpenID Connect support to the PHP League's OAuth2 Server. Compatible with Laravel Passport.

59437.0k9](/packages/jeremy379-laravel-openid-connect)[api-platform/laravel

API Platform support for Laravel

58171.6k14](/packages/api-platform-laravel)[duncanmcclean/statamic-cargo

Comprehensive e-commerce addon for Statamic. Build bespoke e-commerce sites without the complexity.

3417.0k](/packages/duncanmcclean-statamic-cargo)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
