在 Laravel 5+ 中从数据库中删除列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37631682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing column from database in Laravel 5+
提问by byteseeker
I've a blog for which the articles
table Schema
is defined like this:
我有一个博客,该articles
表Schema
的定义如下:
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('title');
$table->string('thumb')->nullable();
$table->text('excerpt');
$table->text('body');
$table->string('slug')->unique();
$table->integer('comment_count')->unsigned()->default(0);
$table->integer('view_count')->unsigned()->default(0);
$table->timestamps();
$table->softDeletes();
}
public function down()
{
Schema::drop('articles');
}
I want to drop the columns comment_count
and view_count
without losing existing data in the table
我要删除的列comment_count
,并view_count
没有在表中丢失现有数据
I defined a new migration like this:
我定义了一个这样的新迁移:
class RemoveCommentViewCount extends Migration
{
public function up()
{
//nothing here
}
public function down()
{
Schema::table('articles', function($table) {
$table->dropColumn('comment_count');
$table->dropColumn('view_count');
});
}
}
and I did php artisan migrate
. It did migrate successfully, but the two columns are not dropped.
我做到了php artisan migrate
。它确实迁移成功,但没有删除两列。
What am I doing wrong? How can I drop those columns without losing the existing data in the table?
我究竟做错了什么?如何在不丢失表中现有数据的情况下删除这些列?
回答by Sangar82
Your migration must look like this:
您的迁移必须如下所示:
Class RemoveCommentViewCount extends Migration
{
public function up()
{
Schema::table('articles', function($table) {
$table->dropColumn('comment_count');
$table->dropColumn('view_count');
});
}
public function down()
{
Schema::table('articles', function($table) {
$table->integer('comment_count');
$table->integer('view_count');
});
}
}
The dropColumn in the up method, because with new migration you want to delete this columns. If you make a rollback, you have another time the two columns
up 方法中的 dropColumn,因为在新的迁移中您要删除此列。如果您进行回滚,则两列还有一次
回答by Channaveer Hakari
Even you can drop the multiple columns in a single line by passing the array column to dropColumnfunction.
即使您可以通过将数组列传递给 dropColumn函数,将多列放在一行中。
Class RemoveCommentViewCount extends Migration
{
public function up()
{
Schema::table('articles', function($table) {
$table->dropColumn(['comment_count', 'view_count']);
});
}
public function down()
{
Schema::table('articles', function($table) {
$table->integer('comment_count');
$table->integer('view_count');
});
}
}
In case if your having foregin keyconstraint then first drop the foreign key index assocation and then can pass the column to dropColumn function with others at a strech like following
如果您有外键约束,则首先删除外键索引关联,然后可以将列传递给 dropColumn 函数,以如下方式与其他人一起使用
public function up()
{
Schema::table('customer_orders', function($table) {
$table->dropForeign(['product_id']);
$table->dropForeign(['shipping_address_id']);
$table->dropColumn(['product_id', 'shipping_address_id', 'column1', 'column2']);
});
}
回答by FloMathew Wahome
I had a similar issue, I edited the initial migration, that is the initial table schema, removed the columns and then run php artisan migrate:refreshand it worked for me
我有一个类似的问题,我编辑了初始迁移,即初始表架构,删除了列,然后运行php artisan migrate:refresh并且它对我有用
回答by Vaibhav Bacchav
Just add this code into your down()function in migration.php file
只需将此代码添加到migration.php 文件中的down()函数中
Schema::table('articles', function (Blueprint $table) {
$table->integer('comment_count')->unsigned()->default(0);
$table->integer('view_count')->unsigned()->default(0);
});
and then run --> php artisan migrate:rollback
然后运行 --> php artisan migrate:rollback