Php 如何在Laravel中修改迁移?

Php 如何在Laravel中修改迁移?,php,laravel,migration,Php,Laravel,Migration,我正在尝试修改现有的迁移。以下是我当前的迁移类: class CreateLogForUserTable extends Migration { public function up() { Schema::create('log_for_user', function (Blueprint $table) { $table->increments('id'); $table->integer('user

我正在尝试修改现有的迁移。以下是我当前的迁移类:

class CreateLogForUserTable extends Migration
{
    public function up()
    {
        Schema::create('log_for_user', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->string('table_name');
            $table->string('error_message');
            $table->unsignedTinyInteger('error_code');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('log_for_user');
    }
}
我已经执行了一次
php artisan migrate
命令。现在我需要将
->nullable()
方法添加到
error\u message
列中。所以我编辑了我的迁移,类似这样:

.
.
    $table->string('error_message')->nullable();
.
.
class UpdateLogForUserTable extends Migration
{
    public function up()
    {
        Schema::table('log_for_user', function (Blueprint $table) {
            $table->string('error_message')->nullable()->change();
        });
    }

    public function down()
    {
        Schema::table('log_for_user', function (Blueprint $table) {
            $table->string('error_message')->change();
        });
    }
}
但是,当我再次执行
php artisan migrate
时,它会说:

没有什么可以迁移的


如何应用迁移的新版本?

您应该使用以下命令创建新迁移:

php artisan make:migration update_error_message_in_log_for_user_table
然后,在创建的迁移类中,使用
change
方法添加此行,如下所示:

.
.
    $table->string('error_message')->nullable();
.
.
class UpdateLogForUserTable extends Migration
{
    public function up()
    {
        Schema::table('log_for_user', function (Blueprint $table) {
            $table->string('error_message')->nullable()->change();
        });
    }

    public function down()
    {
        Schema::table('log_for_user', function (Blueprint $table) {
            $table->string('error_message')->change();
        });
    }
}
要进行这些更改并运行迁移,请使用以下命令:

php artisan migrate
php artisan migrate:rollback
要回滚更改,请使用以下命令:

php artisan migrate
php artisan migrate:rollback
通过向rollback命令提供
step
选项,可以回滚有限数量的迁移。例如,以下命令将回滚最后五次迁移:

php artisan migrate:rollback --step=5
了解更多关于

您可以使用该方法,它允许您将某些现有列类型修改为新类型或修改列的属性

例如,将列修改为可为空:

Schema::table('log_for_user', function ($table) {
    $table->string('error_message')->nullable()->change();
});
但首先,您需要
原则/dbal


如果您的应用程序未投入生产,而您正在为数据播种,那么您最好运行:

php artisan migrate:refresh --seed
此命令将删除所有表并重新创建它们。然后它将为数据播种


如果在开发过程中为每个更改创建额外的迁移,那么最终将有数百个迁移类。

有两种方法可以做到这一点:

  • 运行php artisan migrate:refresh。这将回滚所有的 迁移和迁移您的所有迁移。如果运行此命令, 插入数据库的所有数据都将丢失
  • 运行
    php artisan make:migration在此处输入您的\u migration\u name\u
    。 然后在迁移中插入以下内容:

    $table->string('error_message')->nullable()->change()

    然后运行
    php artisan migrate
    对表进行更改。(请注意,执行此操作时,您的composer中有require
    composer require-doctrine/dbal


  • 还有一个选择。回滚迁移,编辑文件,然后再次运行

    在本地开发服务器上解决新代码中的bug时,这是一件非常常见的事情。使用已接受答案中的方法,您可能会完成17次迁移以创建一个表

    php artisan迁移 #意识到你犯了一个错误 php artisan迁移:回滚 #编辑迁移文件 php artisan迁移
    如果需要,可以在命令行上指定要执行的后退步数

    #撤消最后3次迁移
    php artisan迁移:回滚--步骤=3
    
    也可以指定需要撤消的特定迁移

    #撤消一个特定的迁移
    php artisan migrate:rollback--path=./database/migrations/2014_10_12_100000_create_users_table.php
    
    在这种情况下,当前数据集将被删除,对吗?是的,数据将丢失。但正确的方法是在开发过程中播种数据,而不是使用真实的数据。我明白了。只要一件事,
    --seed
    到底做了什么?我知道
    迁移:refresh
    做什么,但我不知道
    --seed
    做什么..它运行seeders,与
    php artisan db:seed
    命令的作用相同。你可以读一下。老实说,对我来说,这个数据库还是很模糊的。。“种子”在数据库中的确切含义是什么?它是否用不真实的数据填充数据库?那么这些数据来自哪里呢?只有一个问题,我应该在什么时候使用
    change()
    方法?如果我想添加一个索引(比如
    unique()
    ),而不是
    nullable()
    ,我还需要像这样使用
    change()
    <代码>$table->string('error_message')->unique()->change()Yes,
    change
    方法告诉laravel修改那个特定的查询@stack-根据laravel perquisite安装此软件包-
    composer Required条令/dbal
    和我的最后一个问题,我如何才能退一步?我的意思是,假设我创建了一个新的迁移,添加了预期的更改,并执行了
    php artisan migrate
    ,我现在很遗憾,我该如何返回一步呢?只有一个问题,我到底应该在什么时候使用
    change()
    方法?如果我想添加一个索引(比如
    unique()
    ),而不是
    nullable()
    ,我还需要像这样使用
    change()
    <代码>$table->string('error_message')->unique()->change()您不需要为
    唯一性
    更改
    方法。只需像这样使用
    $table->unique('error_message')在一个新的迁移文件中。它是“composer require doctor/dbal”(您缺少一个字母)