You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel集成Stripe订阅报错:User::createSetupIntent()方法未定义

解决Laravel Cashier集成Stripe时Call to undefined method App\Models\User::createSetupIntent()错误

你遇到的这个错误核心原因很明确:你的User模型没有启用Laravel Cashier提供的Billable trait。虽然你已经在模型顶部引入了Laravel\Cashier\Billable,但没有在类内部通过use声明来激活这个trait,而createSetupIntent()正是Billable trait提供的核心方法之一。

修复步骤:

修改你的User.php模型,在trait列表中添加Billable:

<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Laravel\Cashier\Billable;

class User extends Authenticatable
{
    // 在这里添加Billable trait
    use HasApiTokens, HasFactory, Notifiable, Billable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'role',
        'plan',
        'created_by',
        'status',
    ];

    // ... 其他代码保持不变
}

额外验证步骤:

  1. 确保已经执行过Cashier的迁移命令(如果还没执行):
php artisan migrate

该命令会创建Cashier处理订阅所需的相关数据表。

  1. 清除应用缓存,避免类加载缓存导致的问题:
php artisan optimize:clear

添加Billable trait后,不仅createSetupIntent()能正常调用,你代码中用到的newSubscription()方法(同样来自该trait)也能正常工作。

内容的提问来源于stack exchange,提问作者Muhammad Arslan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 19:02:11