5.2

2016-06-04 2 views
13

나는 articles 테이블 Schema는 다음과 같이 정의되는 블로그했습니다 :5.2

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했다. 이주는 성공적 이었지만 두 컬럼은 h 제되지 않았습니다.

내가 뭘 잘못하고 있니? 테이블의 기존 데이터를 손실하지 않고 해당 열을 삭제하려면 어떻게해야합니까?

+1

@ Sangar82에 의해 지적 되었 듯이, 나는'php artisan migrate'를 실행할 때 트리거되는'up()'메소드입니다. 'down()'메소드는 아래에 설명 된 것처럼'migrate : refresh' 또는'rollback'을 할 경우에 트리거됩니다. – daneczech

답변

24

마이그레이션은 다음과 같이해야합니다

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

dropColumn을 위 방법으로, 새로운 마이그레이션이 열을 삭제할 때문이다.

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

다음 실행 migration.php 파일은 롤백을하면, 당신은

+0

효과가있었습니다. 고맙습니다. +1 – byteseeker

+2

dropColumn 메서드에 열 이름 배열을 전달하여 테이블에서 여러 열을 삭제할 수 있습니다. $ table-> dropColumn ([ 'votes', 'avatar', 'location']); – mcmacerson

0

그냥 아래로 에이 코드를 추가하려면 두 개의 열이 다른 시간을 가지고() 기능 ->PHP 장인 migrate : rollback