如何将Laravel迁移字段克隆到另一个迁移中?

时间:2020-05-28 09:57:40

标签: laravel laravel-6 laravel-migrations

我有这个Laravel迁移结构:

class CreateWarehouseProductTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('warehouse_products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('product_id')->default(0);
            $table->integer('warehouse_id');
            $table->integer('free_amount')->default(0);
            $table->integer('booked_amount')->default(0);
            // ...
            $table->timestamps();
        });
    }

    // ...
}

我需要从仓库产品创建每日备份,为此,我需要创建一个与warehouse_products完全相同的新表,并包含一个额外的字段作为备份日期。

是否有最佳做法?我认为是这样的:

class CreateWarehouseProductBackupTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('warehouse_product_backups', function (Blueprint $table) {
            CreateWarehouseProductTable->cloneFieldsToHere();
            $table->date('date_of_backup');
        });
    }
}

是否有类似的良好做法来克隆现有迁移中的字段?

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,我认为它并不优雅,但是可以正常工作:

int