Wallogit.com
2017 © Pedro Peláez
Laravel 订阅模块,用于有时间限制的服务订阅
订阅模块,用于有时间限制的服务订阅, (*1)
通过composer安装, (*2)
composer require goodwong/laravel-subscription
打开config/app.php,在providers数组里注册服务:, (*3)
Goodwong\LaravelSubscription\SubscriptionServiceProvider::class,
创建数据库, (*4)
php artisan migrate
php
$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' => '',
]);
查询订阅, (*5)
// 有global scope限定start_at/end_at
Goodwong\LaravelSubscription\Entities\Subscription::where('user_id', $user_id)->first();
// 查询所有订阅(包含已经归档的)
Goodwong\LaravelSubscription\Entities\Subscription::withoutGlobalScopes()->withTrashed()->get();
与User结合, (*6)
<?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');
});
}
}