简体   繁体   English

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

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

When I drop a column, I use following command to create migration file,当我删除一列时,我使用以下命令创建迁移文件,

php artisan make:migration drop_institue_id_column_from_providers php artisan make:migration drop_institue_id_column_from_providers

And migration file:和迁移文件:

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

If I want to drop multiple columns, I can write migration file this way,如果我想删除多个列,我可以这样写迁移文件,

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

But What is the command to create that migration file?但是创建该迁移文件的命令是什么? Thank you.谢谢你。

I assume this is just a question about naming convention for the migration class.我认为这只是关于迁移类的命名约定的问题。

If so then it does not matter what the file\\class is called, as long as it gives you a good idea of what it does.如果是这样,那么 file\\class 被称为什么并不重要,只要它让您对它的作用有一个很好的了解。

So it could be something like:所以它可能是这样的:

php artisan make:migration drop_institue_columns_from_providers php artisan make:migration drop_institue_columns_from_providers

When you execute the php artisan make:migration command, it just create a class in which you can specific what migration you can do.当您执行php artisan make:migration命令时,它只会创建一个类,您可以在其中指定您可以执行的迁移。 You can do many modification for one table, even for many table.您可以对一张表进行多次修改,甚至可以对多张表进行修改。 So just put all your code in one migration file will be OK.所以只需将所有代码放在一个迁移文件中就可以了。

To create a migration file run:要创建迁移文件,请运行:

php artisan make: migration your_migration_file_name_here

and then you can drop multiple columns by passing an array然后你可以通过传递一个数组来删除多列

 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