goodwong / laravel-subscription by goodwong

Laravel 订阅模块,用于有时间限制的服务订阅
18
1
2
Package Data
Maintainer Username: goodwong
Maintainer Contact: goodwong@foxmail.com (william)
Package Create Date: 2017-06-17
Package Last Update: 2017-08-28
Language: PHP
License: MIT
Last Refreshed: 2024-11-23 03:03:49
Package Statistics
Total Downloads: 18
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 1
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

Laravel 5 Subscription

订阅模块,用于有时间限制的服务订阅

安装

  1. 通过composer安装

    composer require goodwong/laravel-subscription
    
  2. 打开config/app.php,在providers数组里注册服务:

    Goodwong\LaravelSubscription\SubscriptionServiceProvider::class,
    
  3. 创建数据库

    php artisan migrate
    

操作

  1. 为用户添加订阅服务(在相同type下,若用户已经有效订阅,则会自动删除旧的订阅)

    $handler = app('Goodwong\LaravelSubscription\Handlers\SubscriptionHandler');
    $subscription = $handler->subscribe($user_id, $level = 'basic', $days = 30, $config = [
        'type' => 'plan',
        'start_at' => '2017-05-05 08:00:59',
        'comment' => '',
    ]);
    
  2. 查询订阅

    // 有global scope限定start_at/end_at
    Goodwong\LaravelSubscription\Entities\Subscription::where('user_id', $user_id)->first();
    // 查询所有订阅(包含已经归档的)
    Goodwong\LaravelSubscription\Entities\Subscription::withoutGlobalScopes()->withTrashed()->get();
    
  3. 与User结合

    <?php
    
    namespace App\User\Entities;
    
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Database\Eloquent\Builder;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    
    class User extends Authenticatable
    {
        use Notifiable;
    
        /**
         * membership
         */
        public function plan()
        {
            return $this->hasOne('Goodwong\LaravelSubscription\Entities\Subscription')
                // ->where('type', 'plan')
                ->orderBy('id', 'desc')
                ;
        }
    
        /**
         * The "booting" method of the model.
         *
         * @return void
         */
        protected static function boot()
        {
            parent::boot();
    
            static::addGlobalScope('plan', function (Builder $builder) {
                $builder->with('plan');
            });
        }
    }