如何在Laravel中修改迁移

如何在Laravel中修改迁移

本文介绍了如何在Laravel中修改迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改现有的迁移.这是我当前的迁移课程:

I'm trying to modify a existing migration. Here is my current migration class:

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命令一次.现在,我需要在error_message列中添加->nullable()方法.因此,我编辑了迁移文件,例如:

I've executed the php artisan migrate command once. Now I need to add ->nullable() method to the error_message column. So I edited my migration, something like this:

.
.
    $table->string('error_message')->nullable();
.
.

但是当我再次执行php artisan migrate时,它说:

But when I execute php artisan migrate again, it says:

如何应用新版本的迁移?

How can I apply the new version of the migration?

推荐答案

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

You should create a new migration using command:

php artisan make:migration update_error_message_in_log_for_user_table

然后,在创建的迁移类中,使用如下的change方法添加以下行:

Then, in that created migration class, add this line, using the change method like this:

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();
        });
    }
}

要进行这些更改并运行迁移,请使用以下命令:

To make these changes and run the migration, use the command:

php artisan migrate

并要回滚更改,请使用命令:

and to rollback the changes, use the command:

php artisan migrate:rollback

通过向rollback命令提供step选项,可以回滚有限数量的迁移.例如,以下命令将回滚最近的五个迁移:

You may rollback a limited number of migrations by providing the step option to the rollback command. For example, the following command will rollback the last five migrations:

php artisan migrate:rollback --step=5

这篇关于如何在Laravel中修改迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 03:04