我有一个博客,articles table 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');
}

我希望在不丢失表中现有数据的情况下删除列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');
       });
   }
}

and I did php artisan migrate . It did migrate successfully, but the two columns are not dropped.

What am I doing wrong? How can I drop those columns without losing the existing data in the table?

推荐答案

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

up方法中的dropColumn,因为通过新迁移,您希望删除这些列.如果您进行回滚,则有另一个时间来处理这两列

Laravel相关问答推荐

URL类图中的循环关系

如何获取多态多对多关系子查询的自动 id 列名?

使用枢轴插入多对多时的Laravel问题

Laravel 5 - 更改模型文件位置

Laravel 5表单请求验证返回禁止错误

PHP Laravel:如何设置或获取会话数据?

Laravel Electron邮件验证模板位置

需要 Vagrant 环境或目标机器

这个上下文的流程应该是怎样的? (根据 Select 的付款方式,将处理付款的代码放在哪里?)

使用 vue-router 更改路由时中止所有 Axios 请求

如何卸载 Laravel?

如何在 Laravel 5.5 中为选定的请求类设置自定义响应

中间件中的 Laravel 依赖注入

如何在 laravel 中解码 Json 对象并在 laravel 中应用 foreach 循环

如何使用 Laravel 测试授权重定向?

Elastic search全文与mysql全文?

Grammar::parameterize() 必须是数组类型

获取多列字段?

如何在不要求用户登录 Laravel 的情况下验证Electron邮件

Laravel 更新后用户模型错误(用户类包含 3 个抽象方法)