向 Laravel 迁移模式添加字段

sha*_*nex 2 migration laravel-5

我已经阅读了 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');
    }

}
Run Code Online (Sandbox Code Playgroud)

然后,运行命令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');
    }

}
Run Code Online (Sandbox Code Playgroud)

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

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

haa*_*kym 5

我认为这就是您想要做的,请按照以下步骤操作

php artisan make:migration create_products_table

充实您的create_products_table.php(注意:迁移文件前面将带有时间戳,这将确定运行php artisan migrate.

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');
}
Run Code Online (Sandbox Code Playgroud)

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

所以现在你已经创建了你的产品表,但现在你想要改变表并向它添加一些额外的字段。做这个:

php artisan make:migration alter_products_table_add_image_active

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

现在在您的更改迁移中充实您的 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');
    });
}
Run Code Online (Sandbox Code Playgroud)

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

  • 啊!好一个!但必须将 `$table-&gt;int('active');` 改为 `$table-&gt;integer('active');`。非常感谢! (2认同)