Laravel Cashier cancel()抛出BadMethodCallException

I used Laravel Cashier to handle my user's subscription, however when I try to do the basic cancellation $user->subscription('main')->cancel(), an exception is being thrown

BadMethodCallException: Call to undefined method Illuminate\Database\Query\Builder::asStripeCustomer() in

\try\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php:2483

Stack trace:

\try\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1470): Illuminate\Database\Eloquent\Builder->__call('asStripeCustome...', Array)

\try\vendor\laravel\cashier\src\Subscription.php(345): Illuminate\Database\Eloquent\Model->__call('asStripeCustome...', Array)

\try\vendor\laravel\cashier\src\Subscription.php(256): Laravel\Cashier\Subscription->asStripeSubscription()

I setup the Model correctly and I uses the Billable trait so I really have no idea what's really causing this error

App\User.php

use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Cashier\Billable;

class User extends Authenticatable{
   use Billable;

   ...
}

App\Http\Controllers\UserController.php

public function cancelSubscription(Request $request)
{
    $user = $request->user();

    try {
        if ($user->subscription('main')->onTrial()) {
            $user->subscription('main')->cancelNow();
        } else {
            $user->subscription('main')->cancel();
        }
    } catch (\Exception $e) {
        \Log::error($e);
        return [
            'success' => 0,
            'message' => "Something went wrong while trying cancel your subscription. Please try again later."
        ];
    }

Any help and hints will be greatly appreciated, thanks in advance!

My bad, I just found out that it was actually with my stripe configuration on /config/services.php as I have two models for my Users (because I'm also using another package other than laravel-cashier to handle payments via Authorize.net on which I ended up creating different Models for them to work)

'stripe' => [
    // 'model' => App\AnetUser::class, => this actually caused the error as
               // ->asStripeCustomer() doesn't exists on an Authorize.net's Billable trait 
    'model' => App\User::class,
    'key' => env('STRIPE_KEY'),
    'secret' => env('STRIPE_SECRET'),
],

I feel so stupid. XD

Hi I've never worked with Laravel Cashier before but, I think the root of your problem might be that you are accessing user from the request, therefore it's not a user instance thats why it triggers undefined methods errors.

So creating a user instance should probably work out for you:

Note: I don't know if $request->user is primary key or whole user instance so I added different solutions

public function cancelSubscription(Request $request)
{
    // if $request->user is the user instance you can do this:
    $user = App\User::findOrFail($request->user->id);

    // if $request->user was any other field from user you could retrieve
    // the user using something like-> 
   // App\User::where('fieldName', 'LIKE', $request->user)->firstOrFail();


    try {
        if ($user->subscription('main')->onTrial()) {
            $user->subscription('main')->cancelNow();
        } else {
            $user->subscription('main')->cancel();
        }
    } catch (\Exception $e) {
        \Log::error($e);
        return [
            'success' => 0,
            'message' => "Something went wrong while trying cancel your subscription. Please try again later."
        ];
    }
}