首页 > 解决方案 > Laravel - 使用 php artisan make:migration 一次将两列添加到现有表中

问题描述

我是新手laravel,我想知道我们不能使用将两列添加到现有表中吗

php artisan make:migration 

立即为 Ex。如果我的用户表包含iduser_name现在我想添加两个新列,如 asuser_phoneuser_emailin one

php artisan make:migration add_user_phone_to_users_table add_user_email_to_users_table 

类似的东西?非常抱歉,如果我的问题是错误的。我可以将新字段一一添加到两个单独的迁移中,但想知道是否可以一次将两个新列添加到现有表中。在此先感谢,我希望我能得到满意的答复。

标签: laravelmigration

解决方案


创建一个新的迁移是对的,php artisan make:migration add_email_and_phone_number_to_users --table=users

在迁移中,您可以为此添加代码:

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->string('email')->nullable();
        $table->string('phone_number')->nullable();
    });
}

public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropColumn(['email', 'phone_number']);
    });
}

推荐阅读