Laravel 5.4 Cashier添加订阅

时间:2017-08-08 04:04:05

标签: php laravel laravel-5 laravel-cashier

我完全按照Laravel文档通过Cashier创建新订阅。我的控制器看起来像这样:

public function saveSubscription(Request $request)
{
    $plan = Plan::find($request->input('plan'));
    //$user_id = Auth::id();
    //$user = User::find($user_id); // work but ugly
    //$user = $request->user(); // same error
    $user = Auth::user();

    $user->newSubscription($plan->name, $plan->name)->create($request->input('stripeToken'), [
        'email' => $request->input('stripeEmail'),
    ]);

所有这些尝试从UserAuth获取Request模型失败,并出现以下错误:

Call to undefined method Illuminate\Database\Query\Builder::newSubscription()

唯一有效的方法是通过Auth::id()电话获取用户ID,然后手动查找。

我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

您需要将Billable特征添加到您的用户模型

use Laravel\Cashier\Billable;

class User extends Authenticatable
{
    use Billable;
}