{"id":17922696,"url":"https://github.com/overtrue/laravel-subscribe","last_synced_at":"2025-05-15T18:04:32.060Z","repository":{"id":38684051,"uuid":"254428935","full_name":"overtrue/laravel-subscribe","owner":"overtrue","description":":email: User Subscribe/Unsubscribe features for Laravel Application.","archived":false,"fork":false,"pushed_at":"2025-03-23T05:04:24.000Z","size":49,"stargazers_count":178,"open_issues_count":0,"forks_count":12,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-31T22:18:22.009Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/overtrue.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["overtrue"]}},"created_at":"2020-04-09T16:51:07.000Z","updated_at":"2025-03-28T22:26:06.000Z","dependencies_parsed_at":"2024-02-17T06:32:13.801Z","dependency_job_id":"d0775a19-c20d-476a-aa8c-a8f90daeb8cd","html_url":"https://github.com/overtrue/laravel-subscribe","commit_stats":{"total_commits":27,"total_committers":5,"mean_commits":5.4,"dds":0.2222222222222222,"last_synced_commit":"1ddb7c3ac5c1e145364ef5736f8912eb5dd2d707"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-subscribe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-subscribe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-subscribe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-subscribe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/overtrue","download_url":"https://codeload.github.com/overtrue/laravel-subscribe/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247744332,"owners_count":20988783,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-28T20:40:29.101Z","updated_at":"2025-04-07T23:07:57.928Z","avatar_url":"https://github.com/overtrue.png","language":"PHP","funding_links":["https://github.com/sponsors/overtrue"],"categories":[],"sub_categories":[],"readme":"Laravel Subscribe\n---\n\n:email: User subscribe/unsubscribe feature for Laravel Application.\n\n![CI](https://github.com/overtrue/laravel-subscribe/workflows/CI/badge.svg)\n\n\n[![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me-button-s.svg?raw=true)](https://github.com/sponsors/overtrue)\n\n## Installing\n\n```shell\n$ composer require overtrue/laravel-subscribe -vvv\n```\n\n### Configuration\n\nThis step is optional\n\n```php\n$ php artisan vendor:publish --provider=\"Overtrue\\\\LaravelSubscribe\\\\SubscribeServiceProvider\" --tag=config\n```\n\n### Migrations\n\n**You need to publish the migration files for use the package:**\n\n```php\n$ php artisan vendor:publish --provider=\"Overtrue\\\\LaravelSubscribe\\\\SubscribeServiceProvider\" --tag=migrations\n```\n\n\n## Usage\n\n### Traits\n\n#### `Overtrue\\LaravelSubscribe\\Traits\\Subscriber`\n\n```php\n\nuse Illuminate\\Notifications\\Notifiable;\nuse Illuminate\\Contracts\\Auth\\MustVerifyEmail;\nuse Illuminate\\Foundation\\Auth\\User as Authenticatable;\nuse Overtrue\\LaravelSubscribe\\Traits\\Subscriber;\n\nclass User extends Authenticatable\n{\n    use Subscriber;\n    \n    \u003c...\u003e\n}\n```\n\n#### `Overtrue\\LaravelSubscribe\\Traits\\Subscribable`\n\n```php\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Overtrue\\LaravelSubscribe\\Traits\\Subscribable;\n\nclass Post extends Model\n{\n    use Subscribable;\n\n    \u003c...\u003e\n}\n```\n\n### API\n\n```php\n$user = User::find(1);\n$post = Post::find(2);\n\n$user-\u003esubscribe($post);\n$user-\u003eunsubscribe($post);\n$user-\u003etoggleSubscribe($post);\n\n$user-\u003ehasSubscribed($post); \n$post-\u003eisSubscribedBy($user); \n```\n\n##### Get object subscribers:\n\n```php\nforeach($post-\u003esubscribers as $user) {\n    // echo $user-\u003ename;\n}\n```\n\n##### Aggregations\n\n```php\n// all\n$user-\u003esubscriptions()-\u003ecount(); \n\n// with type\n$user-\u003esubscriptions()-\u003ewithType(Post::class)-\u003ecount(); \n\n// subscribers count\n$post-\u003esubscribers()-\u003ecount();\n```\n\nList with `*_count` attribute:\n\n```php\n$users = User::withCount('subscriptions')-\u003eget();\n\nforeach($users as $user) {\n    echo $user-\u003esubscriptions_count;\n}\n```\n\n### Filter subscribables\n\n```php\n$posts = Post::hasSubscribers($user)-\u003eget();\n$posts = Post::hasSubscribers($user-\u003eid)-\u003eget();\n$posts = Post::hasSubscribers([$user1, $user2])-\u003eget();\n$posts = Post::hasSubscribers([$user1-\u003eid, $user2-\u003eid])-\u003eget();\n\n// or\n$posts = Post::subscribedBy($user)-\u003eget();\n$posts = Post::subscribedBy($user-\u003eid)-\u003eget();\n$posts = Post::subscribedBy([$user1, $user2])-\u003eget();\n$posts = Post::subscribedBy([$user1-\u003eid, $user2-\u003eid])-\u003eget();\n```\n\n### Order by subscribers count\n\nYou can query subscribable model order by subscribers count with following methods:\n\n- `orderBySubscribersCountDesc()`\n- `orderBySubscribersCountAsc()`\n- `orderBySubscribersCount(string $direction = 'desc')`\n\nexample: \n\n```php\n$posts = Post::orderBySubscribersCountDesc()-\u003eget();\n$mostPopularPost = Post::orderBySubscribersCountDesc()-\u003efirst();\n```\n\n### N+1 issue\n\nTo 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:\n\n```php\n// Subscriber\n$users = App\\User::with('subscriptions')-\u003eget();\n\nforeach($users as $user) {\n    $user-\u003ehasSubscribed($post);\n}\n\n// Subscribable\n$posts = App\\Post::with('subscriptions')-\u003eget();\n// or \n$posts = App\\Post::with('subscribers')-\u003eget();\n\nforeach($posts as $post) {\n    $post-\u003eisSubscribedBy($user);\n}\n```\n\n### Attach the subscription status to subscribable collection\n\nYou can use `Subscriber::attachSubscriptionStatus(Collection $subscribeables)` to attach the user subscription status, it will set `has_subscribed` attribute to each model of `$subscribables`:\n\n#### For model\n\n```php\n$user1 = User::find(1);\n\n$user-\u003eattachSubscriptionStatus($user1);\n\n// result\n[\n    \"id\" =\u003e 1\n    \"name\" =\u003e \"user1\"\n    \"private\" =\u003e false\n    \"created_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"updated_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"has_subscribed\" =\u003e true  \n  ]\n```\n\n#### For `Collection | Paginator | LengthAwarePaginator | array`:\n\n```php\n$user = auth()-\u003euser();\n\n$posts = Post::oldest('id')-\u003eget();\n\n$posts = $user-\u003eattachSubscriptionStatus($posts);\n\n$posts = $posts-\u003etoArray();\n\n// result\n[\n  [\n    \"id\" =\u003e 1\n    \"title\" =\u003e \"title 1\"\n    \"created_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"updated_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"has_subscribed\" =\u003e true  \n  ],\n  [\n    \"id\" =\u003e 2\n    \"title\" =\u003e \"title 2\"\n    \"created_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"updated_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"has_subscribed\" =\u003e true\n  ],\n  [\n    \"id\" =\u003e 3\n    \"title\" =\u003e \"title 3\"\n    \"created_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"updated_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"has_subscribed\" =\u003e false\n  ],\n  [\n    \"id\" =\u003e 4\n    \"title\" =\u003e \"title 4\"\n    \"created_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"updated_at\" =\u003e \"2021-06-07T15:06:47.000000Z\"\n    \"has_subscribed\" =\u003e false\n  ],\n]\n```\n\n#### For pagination\n\n```php\n$posts = Post::paginate(20);\n\n$user-\u003eattachSubscriptionStatus($posts);\n```\n\n### Events\n\n| **Event** | **Description** |\n| --- | --- |\n|  `Overtrue\\LaravelSubscribe\\Events\\Subscribed` | Triggered when the relationship is created. |\n|  `Overtrue\\LaravelSubscribe\\Events\\Unsubscribed` | Triggered when the relationship is deleted. |\n\n## Related packages\n\n- Follow: [overtrue/laravel-follow](https://github.com/overtrue/laravel-follow)\n- Like: [overtrue/laravel-like](https://github.com/overtrue/laravel-like)\n- Favorite: [overtrue/laravel-favorite](https://github.com/overtrue/laravel-favorite)\n- Subscribe: [overtrue/laravel-subscribe](https://github.com/overtrue/laravel-subscribe)\n- Vote: [overtrue/laravel-vote](https://github.com/overtrue/laravel-vote)\n- Bookmark: overtrue/laravel-bookmark (working in progress)\n\n## :heart: Sponsor me \n\n[![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me.svg?raw=true)](https://github.com/sponsors/overtrue)\n\n如果你喜欢我的项目并想支持它，[点击这里 :heart:](https://github.com/sponsors/overtrue)\n\n## Project supported by JetBrains\n\nMany thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.\n\n[![](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)](https://www.jetbrains.com/?from=https://github.com/overtrue)\n\n\n## Contributing\n\nYou can contribute in one of three ways:\n\n1. File bug reports using the [issue tracker](https://github.com/overtrue/laravel-subscribes/issues).\n2. Answer questions or fix bugs on the [issue tracker](https://github.com/overtrue/laravel-subscribes/issues).\n3. Contribute new features or update the wiki.\n\n_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._\n\n## PHP 扩展包开发\n\n\u003e 想知道如何从零开始构建 PHP 扩展包？\n\u003e\n\u003e 请关注我的实战课程，我会在此课程中分享一些扩展开发经验 —— [《PHP 扩展包实战教程 - 从入门到发布》](https://learnku.com/courses/creating-package)\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fovertrue%2Flaravel-subscribe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fovertrue%2Flaravel-subscribe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fovertrue%2Flaravel-subscribe/lists"}