Search code examples
phplaravellaravel-5laravel-cashier

Laravel 5.4 Cashier add subscription


I've followed Laravel docs exactly for creating a new subscription via Cashier. My controller looks like this:

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'),
    ]);

All of these attempts to get the User model from the Auth or Request fail with the following error:

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

The only thing that works is getting the user ID via the Auth::id() call and then looking it up manually.

Am I missing something?


Solution

  • You need to add the Billable trait to your User model

    use Laravel\Cashier\Billable;
    
    class User extends Authenticatable
    {
        use Billable;
    }