goodwong / laravel-subscription
Laravel 订阅模块,用于有时间限制的服务订阅
v1.0.0
2017-06-17 09:07 UTC
This package is not auto-updated.
Last update: 2024-11-24 03:45:42 UTC
README
订阅模块,用于有时间限制的服务订阅
安装
-
通过composer安装
composer require goodwong/laravel-subscription
-
打开config/app.php,在providers数组里注册服务:
Goodwong\LaravelSubscription\SubscriptionServiceProvider::class,
-
创建数据库
php artisan migrate
操作
-
为用户添加订阅服务(在相同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' => '', ]);
-
查询订阅
// 有global scope限定start_at/end_at Goodwong\LaravelSubscription\Entities\Subscription::where('user_id', $user_id)->first(); // 查询所有订阅(包含已经归档的) Goodwong\LaravelSubscription\Entities\Subscription::withoutGlobalScopes()->withTrashed()->get();
-
与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'); }); } }