Laravel - 使用 php artisan make:migration 立即将两列添加到现有表中

son*_*ade 1 migration laravel

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

php artisan make:migration 
Run Code Online (Sandbox Code Playgroud)

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

php artisan make:migration add_user_phone_to_users_table add_user_email_to_users_table 
Run Code Online (Sandbox Code Playgroud)

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

Thi*_*wes 6

你创建一个新的迁移是对的,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']);
    });
}
Run Code Online (Sandbox Code Playgroud)