无法声明类,因为名称已被使用?

问题描述

我将 Laravel 8 与 Spark 一起使用。我正在设置 Stripe(通过文档)并遇到一个我无法理解的错误

在终端中,尝试 PHP artisan migrate 我收到以下错误:无法声明类 CreateReceiptsTable,因为名称已被使用。

我尝试了 PHP artisan config:cachecomposer dump_autoload,但运气不佳。

这是一个相当新的安装,所以我不明白为什么会发生这个错误

编辑:我尝试过的事情 - PHP artisan migrate:fresh PHP artisan migrate:rollback 但是没有解决问题。

希望有人能帮忙。

创建收据表:


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateReceiptsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('receipts',function (Blueprint $table) {
            $table->id();
            $table->foreignId('billable_id');
            $table->string('billable_type');
            $table->unsignedBigInteger('paddle_subscription_id')->nullable()->index();
            $table->string('checkout_id');
            $table->string('order_id')->unique();
            $table->string('amount');
            $table->string('tax');
            $table->string('currency',3);
            $table->integer('quantity');
            $table->string('receipt_url')->unique();
            $table->timestamp('paid_at');
            $table->timestamps();

            $table->index(['billable_id','billable_type']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('receipts');
    }
}

日志详情

"} 
[2021-05-29 12:10:04] local.ERROR: Cannot declare class CreateReceiptsTable,because the name is already in use {"exception":"[object] (Symfony\\Component\\ErrorHandler\\Error\\FatalError(code: 0): Cannot declare class CreateReceiptsTable,because the name is already in use at /Users/dad/Desktop/projects/Sumpaper/vendor/laravel/spark-stripe/database/migrations/2019_06_03_000003_create_receipts_table.PHP:7)
[stacktrace]
#0 {main}
"} 

解决方法

听起来您已经运行了迁移,并且表已经存在。

如果这是开发环境,请运行 php artisan migrate:fresh 以获取数据库的新副本。不要在生产实例上执行此操作,因为它会擦除所有数据。

如果是生产环境,则需要创建新的迁移来对现有表进行更改,而不是编辑现有的迁移。