定制 overtrue/laravel-subscribe 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

overtrue/laravel-subscribe

Composer 安装命令:

composer require overtrue/laravel-subscribe

包简介

User Subscribe/Unsubscribe features for Laravel Application.

README 文档

README

📧 User subscribe/unsubscribe feature for Laravel Application.

CI

Sponsor me

Installing

$ composer require overtrue/laravel-subscribe -vvv

Configuration

This step is optional

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

Migrations

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

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

Usage

Traits

Overtrue\LaravelSubscribe\Traits\Subscriber

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

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

class Post extends Model
{
    use Subscribable;

    <...>
}

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:
foreach($post->subscribers as $user) {
    // echo $user->name;
}
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

$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

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

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

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

$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:

$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

$posts = Post::paginate(20);

$user->attachSubscriptionStatus($posts);

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

❤️ Sponsor me

Sponsor me

如果你喜欢我的项目并想支持它,点击这里 ❤️

Project supported by JetBrains

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

Contributing

You can contribute in one of three ways:

  1. File bug reports using the issue tracker.
  2. Answer questions or fix bugs on the issue tracker.
  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 扩展包实战教程 - 从入门到发布》

License

MIT

overtrue/laravel-subscribe 适用场景与选型建议

overtrue/laravel-subscribe 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 50.08k 次下载、GitHub Stars 达 183, 最近一次更新时间为 2020 年 04 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 overtrue/laravel-subscribe 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 overtrue/laravel-subscribe 我们能提供哪些服务?
定制开发 / 二次开发

基于 overtrue/laravel-subscribe 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 50.08k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 183
  • 点击次数: 38
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 183
  • Watchers: 4
  • Forks: 15
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-10