将字段添加到 Laravel 迁移架构

Adding field to Laravel migration schema

我已经阅读了 Laravel 的文档和其他论坛,但它对我不起作用。我已成功迁移 table,现在我想添加一个字段,将架构更改为 'table' 但我得到的只是 'Nothing to 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::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 完成了工作,但这不是我想要的,因为它也删除了数据。

我认为这就是你想要做的,请按顺序执行以下步骤

php artisan make:migration create_products_table

充实您 create_products_table.php 中的 up 和 down 方法(注意: 迁移文件前面将带有时间戳,这将决定执行顺序运行 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');
}

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

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

php artisan make:migration alter_products_table_add_image_active

现在你有一个单独的迁移来改变 table,你不想编辑现有的,这不是迁移的工作方式。它们本质上保留了数据库构建方式的历史记录,因此任何调整都应放入新的迁移中,因此如果您碰巧回滚,您可以恢复到之前的状态,这是通过使用 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时,我们可以在migrate命令中使用--table,在菜单table migrate中示例

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