繁体   English   中英

Laravel:如何创建迁移文件以从表中删除多个列?

[英]Laravel: How to create migration file for drop multiple columns from a table?

当我删除一列时,我使用以下命令创建迁移文件,

php artisan make:migration drop_institue_id_column_from_providers

和迁移文件:

public function up()
{
    Schema::table('providers', function(Blueprint $table) {
          $table->dropColumn('institue_id');
    });
}


public function down()
{
    Schema::table('providers', function (Blueprint $table)
    {
        $table->integer('institue_id')->unsigned(); 
    });
}

如果我想删除多个列,我可以这样写迁移文件,

public function up()
{
    Schema::table('providers', function(Blueprint $table) {
          $table->dropColumn('institue_id');
          $table->dropColumn('institue_name');
          $table->dropColumn('institue_address');
    });
}


public function down()
{
    Schema::table('providers', function (Blueprint $table)
    {
        $table->integer('institue_id')->unsigned(); 
        $table->char('institue_name');
        $table->char('institue_address');
    });
}

但是创建该迁移文件的命令是什么? 谢谢你。

我认为这只是关于迁移类的命名约定的问题。

如果是这样,那么 file\\class 被称为什么并不重要,只要它让您对它的作用有一个很好的了解。

所以它可能是这样的:

php artisan make:migration drop_institue_columns_from_providers

当您执行php artisan make:migration命令时,它只会创建一个类,您可以在其中指定您可以执行的迁移。 您可以对一张表进行多次修改,甚至可以对多张表进行修改。 所以只需将所有代码放在一个迁移文件中就可以了。

要创建迁移文件,请运行:

php artisan make: migration your_migration_file_name_here

然后你可以通过传递一个数组来删除多列

 Schema::table('providers', function(Blueprint $table) {
      $table->dropColumn('institue_id', 'institue_name', 'institue_address');
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM