Laravel 5.4收银员添加订阅

Laravel 5.4 Cashier add subscription

我完全按照 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'),
    ]);

AuthRequest 获取 User 模型的所有这些尝试均失败并出现以下错误:

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

唯一可行的方法是通过 Auth::id() 调用获取用户 ID,然后手动查找。

我是不是漏掉了什么?

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

use Laravel\Cashier\Billable;

class User extends Authenticatable
{
    use Billable;
}