14
votes

I need to drop a column from a table on a Laravel migration.

The usual code to do it is:

$table->dropColumn([ 'city', 'country' ]);

But you have to install Doctrine DBAL package to use dropColumn() method.

Looking for another solution, I have found removeColumn() method (http://laravel.com/api/5.0/Illuminate/Database/Schema/Blueprint.html#method_removeColumn):

$table->removeColumn('city'); 
$table->removeColumn('country');

But not seem to work. It does nothing, no fails and leaves intact columns.

What is the difference between removeColumn and dropColumn methods?

Which is the use of removeColumn() method?

1

1 Answers

31
votes

The method removeColumn() is actually pretty useless for most migrations. It just removes the column from the blueprint and not actually from the database. So if you had this migration:

Schema::create('foo', function (Blueprint $table) {
    $table->increments('id');
    $table->timestamps();

    $table->string('foo')->nullable();
    $table->string('bar');

    $table->removeColumn('foo');
});

The created table would not contain the column foo because it is removed from the schema.