【问题标题】:Adding field to Laravel migration schema向 Laravel 迁移模式添加字段
【发布时间】:2015-06-06 11:37:55
【问题描述】:

我已经阅读了 Laravel 的文档和其他论坛,但它对我不起作用。我已经成功迁移了一个表,现在我想添加一个字段,将架构更改为“表”,但我得到的只是“无迁移”。

这就是我所做的。

迁移:

<?php

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

class CreateProductTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('product', function(Blueprint $table)
        {

            $table->text('image');
            $table->integer('stock');
            $table->integer('amount');
            $table->string('color');
            $table->string('dimension');
            $table->integer('ordered');
            $table->timestamps();
        });
    }

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

}

然后,运行命令php artisan migrate,一切正常。

然后我决定添加新字段,所以我将控制器更改为:

<?php

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

class CreateProductTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('product', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->text('description');
            $table->text('image'); //new
            $table->int('active'); //new
            $table->integer('stock');
            $table->integer('amount');
            $table->string('color');
            $table->string('dimension');
            $table->integer('ordered');
            $table->timestamps();
        });
    }

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

}

然后再次运行php artisan migrate,但我只得到Nothing to migrate

我还删除了Blueprint,但效果不佳。 migrate:refreshmigrate:reset 完成了这项工作,但这不是我想要的,因为它也会删除数据。

【问题讨论】:

    标签: migration laravel-5


    【解决方案1】:

    我认为这是您想要做的,请按顺序执行这些步骤

    php artisan make:migration create_products_table

    充实您的 create_products_table.php 中的 up 和 down 方法(注意: 迁移文件前面将带有时间戳,这将确定运行 @987654323 时迁移的执行顺序@。

    public function up()
    {
        Schema::create('product', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->text('description');
            $table->integer('stock');
            $table->integer('amount');
            $table->string('color');
            $table->string('dimension');
            $table->integer('ordered');
            $table->timestamps();
        });
    }
    
    public function down()
    {
        Schema::drop('product');
    }
    

    然后创建你的表运行php artisan migrate

    现在您已经创建了您的产品表,但现在您想要更改该表并向其添加一些额外的字段。这样做:

    php artisan make:migration alter_products_table_add_image_active

    现在您有一个单独的迁移来更改表,您不想编辑现有的,这不是迁移的工作方式。他们基本上保留了数据库构建方式的历史记录,因此任何调整都应该放在新的迁移中,所以如果你碰巧回滚,你可以恢复到以前的状态,这是通过使用 down 方法完成的,这几乎是与 down 方法相反。

    现在在您的 alter 迁移中充实您的 up 和 down 方法,alter_products_table_add_image_active.php注意: 请记住,该文件将自动以时间戳开头)

    public function up()
    {
        Schema::table('product', function(Blueprint $table)
        {
            $table->text('image');
            $table->int('active');
        });
    }
    
    public function down()
    {
        // here you're not dropping the whole table, only removing the newly added columns
        Schema::table('club', function(Blueprint $table){
            $table->dropColumn('image');
            $table->dropColumn('active');
        });
    }
    

    这应该让您启动并运行!如果您遇到任何问题,请告诉我。

    【讨论】:

    • 啊!好一个!但必须将$table-&gt;int('active'); 更改为$table-&gt;integer('active');。非常感谢!
    • 很高兴它为您工作。很快我会更新我的答案没有发现非常感谢。
    【解决方案2】:

    当我们要修改表时,我们可以在迁移命令中使用--table,例如菜单表迁移中的示例

    php artisan make:migrate [your migrate name] --table=menus
    

    【讨论】:

      猜你喜欢
      • 2014-11-24
      • 2012-01-09
      • 2014-08-10
      • 2021-05-13
      • 2018-07-18
      • 2012-05-19
      • 2020-06-13
      • 2016-11-01
      • 2013-05-23
      相关资源
      最近更新 更多