简体   繁体   中英

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

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.

So it could be something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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