Laravel 收银员 cancel() 抛出 BadMethodCallException
Laravel Cashier cancel() throws a BadMethodCallException
我使用 Laravel Cashier 来处理我的用户订阅,但是当我尝试进行基本取消时 $user->subscription('main')->cancel()
,出现了异常
BadMethodCallException: Call to undefined method Illuminate\Database\Query\Builder::asStripeCustomer() in
\try\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php:2483
堆栈跟踪:
\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()
我正确设置了模型并使用了 Billable
特性,所以我真的不知道是什么导致了这个错误
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."
];
}
任何帮助和提示将不胜感激,在此先感谢!
您好,我以前从未与 Laravel Cashier 合作过,但是,我认为您的问题的根源可能是您正在从请求中访问用户,因此它不是用户实例,这就是它触发未定义的原因方法错误。
因此,创建用户实例应该对您有用:
注意:我不知道 $request->user 是主键还是整个用户实例所以我添加了不同的解决方案
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."
];
}
}
糟糕,我刚刚发现它实际上是在 /config/services.php
上使用我的条带配置,因为我有两个模型供我的用户使用(因为我还使用了 laravel-cashier
以外的另一个包通过 Authorize.net 处理付款,我最终创建了不同的模型供他们使用)
'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'),
],
我觉得自己好傻。 XD
我使用 Laravel Cashier 来处理我的用户订阅,但是当我尝试进行基本取消时 $user->subscription('main')->cancel()
,出现了异常
BadMethodCallException: Call to undefined method Illuminate\Database\Query\Builder::asStripeCustomer() in
\try\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php:2483
堆栈跟踪:
\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()
我正确设置了模型并使用了 Billable
特性,所以我真的不知道是什么导致了这个错误
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."
];
}
任何帮助和提示将不胜感激,在此先感谢!
您好,我以前从未与 Laravel Cashier 合作过,但是,我认为您的问题的根源可能是您正在从请求中访问用户,因此它不是用户实例,这就是它触发未定义的原因方法错误。
因此,创建用户实例应该对您有用:
注意:我不知道 $request->user 是主键还是整个用户实例所以我添加了不同的解决方案
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."
];
}
}
糟糕,我刚刚发现它实际上是在 /config/services.php
上使用我的条带配置,因为我有两个模型供我的用户使用(因为我还使用了 laravel-cashier
以外的另一个包通过 Authorize.net 处理付款,我最终创建了不同的模型供他们使用)
'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'),
],
我觉得自己好傻。 XD