在 Laravel5 + 中从数据库中删除列

我有一个博客的 articlesSchema是这样定义的:

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

我希望在不丢失表中现有数据的情况下删除列 comment_countview_count

我定义了这样一个新的迁移:

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

我做了 php artisan migrate。它确实成功地迁移了,但是没有删除这两个列。

我做错了什么? 如何删除这些列而不丢失表中的现有数据?

137210 次浏览

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

Just add this code into your down() function in migration.php file

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

Even you can drop the multiple columns in a single line by passing the array column to dropColumn function.

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 you have a foreign key constraint, then drop first the foreign key index association and then can pass the column to dropColumn function with others like following.

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

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:refresh and it worked for me

Create remove column migration

 php artisan make:migration RemoveCommentViewCount

The down method is for rollbacks, so add dropColumn in your up() function and reverse in down()

<?php
  

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;


class RemoveCommentViewCount extends Migration
{
public function up()
{
if (Schema::hasColumn('comment_count', 'view_count')){
  

Schema::table('articles', function (Blueprint $table) {
$table->dropColumn('comment_count');
$table->dropColumn('view_count');


//OR
$table->dropColumn(['comment_count', 'view_count']);
});
}
}


public function down()
{
Schema::table('articles', function($table) {
$table->integer('comment_count');
$table->integer('view_count');
});


}
}

Check laravel documentation on dropping columns migrations