laravel artisan migrate报类已使用

这是别人写的代码,报错的类的确存在两份,数据库表每次做迁移就会多一个类文件,又没有名称空间
,自然会重复的,该如何搞定啊?

阅读 1.4k
1 个回答
数据库表每次做迁移就会多一个类文件。

为什么会迁移的时候会多出文件?php artisan migrate 不是只会运行 database/migrations 中的迁移文件吗?

对于重复的类名。在最新的 Laravel 9.x 中使用匿名类来解决这个问题

<?php
 
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
 
return new class extends Migration // --------------------------- 直接 return 匿名类
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('airline');
            $table->timestamps();
        });
    }
 
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('flights');
    }
};

source: https://laravel.com/docs/9.x/...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题