从Laravel 5+中的数据库中删除列;

从Laravel 5+中的数据库中删除列;,laravel,Laravel,我有一个博客,其文章表模式定义如下: public function up() { Schema::create('articles', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned(); $table->string('title'); $table->s

我有一个博客,其
文章
模式
定义如下:

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');
}
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');
       });
   }
}
 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');
          });
      }
  }
我想删除列
comment\u count
view\u count
,而不丢失表中的现有数据

我定义了一个新的迁移,如下所示:

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');
}
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');
       });
   }
}
 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');
          });
      }
  }
我做了
php artisan迁移
。它确实成功迁移了,但没有删除这两列


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

您的迁移必须如下所示:

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

up方法中的dropColumn,因为通过新迁移,您希望删除此列。如果您进行回滚,则有另一次,这两列只需将此代码添加到migration.php文件中的down()函数中即可

Schema::table('articles', function (Blueprint $table) {
   $table->integer('comment_count')->unsigned()->default(0);
   $table->integer('view_count')->unsigned()->default(0);
});

然后运行-->php artisan migrate:rollback

即使通过将数组列传递给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');
          });
      }
  }
如果您具有foregin键约束,则首先删除外键索引关联,然后可以按如下顺序将列传递给其他列

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

我遇到了一个类似的问题,我编辑了初始迁移,即初始表模式,删除了列,然后运行php artisan migrate:refresh,这对我很有效创建删除列迁移

 php artisan make:migration RemoveCommentViewCount
down
方法用于回滚,因此在
up()
函数中添加dropColumn,并在
down()中反转


正如@Sangar82所指出的,i是运行
php artisan migrate
时触发的
up()
方法。如果您执行如下所述的
migrate:refresh
rollback
,将触发
down()
方法。您可以通过将列名数组传递给dropColumn方法,从表中删除多个列:$table->dropColumn(['vots',avatar',location']);upvote用于指出外键约束,并且我不必手动键入:)它将删除所有数据。。。