Laravel Framework上的removeColumn和dropColumn方法有什么区别?

Jua*_*bío 8 database database-migration laravel

我需要从Laravel迁移的表中删除一列.

通常的代码是:

$table->dropColumn([ 'city', 'country' ]);
Run Code Online (Sandbox Code Playgroud)

但是你必须安装Doctrine DBAL包使用dropColumn()方法.

寻找另一种解决方案,我找到了removeColumn()方法(http://laravel.com/api/5.0/Illuminate/Database/Schema/Blueprint.html#method_removeColumn):

$table->removeColumn('city'); 
$table->removeColumn('country');
Run Code Online (Sandbox Code Playgroud)

但似乎没有用.它什么都不做,没有失败,留下完整的列.

removeColumn和dropColumn方法有什么区别?

哪个是使用removeColumn()方法?

luk*_*ter 17

removeColumn()对于大多数迁移,该方法实际上毫无用处.它只是从蓝图中删除列,而不是从数据库中删除.所以如果你有这个迁移:

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

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

    $table->removeColumn('foo');
});
Run Code Online (Sandbox Code Playgroud)

创建的表将不包含该列,foo因为它已从架构中删除.