Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between removeColumn and dropColumn methods on Laravel Framework?

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?

like image 962
Juan Antonio Tubío Avatar asked May 15 '15 09:05

Juan Antonio Tubío


People also ask

How do I add a column in laravel migration without losing data?

Show activity on this post. This way a new migration file will be created. Set your column details there, run the migrations using php artisan migrate and that's all. You'll have this new column in your users table without losing previously stored data.


1 Answers

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.

like image 200
lukasgeiter Avatar answered Oct 19 '22 16:10

lukasgeiter