2015-05-16 99 views
1

下面是一些示例代碼:如何在Laravel 5中將列更改爲「不爲空」?

/** 
* Run the migrations. 
* 
* @return void 
*/ 
public function up() 
{ 
    Schema::table('orders', function(Blueprint $table) 
    { 
     $table->integer('user_id')->unsigned()->nullable()->change(); 
    }); 
} 

/** 
* Reverse the migrations. 
* 
* @return void 
*/ 
public function down() 
{ 
    Schema::table('orders', function(Blueprint $table) 
    { 
     $table->integer('user_id')->unsigned()->change(); 
    }); 
} 

第一部分的偉大工程。基本上,我正在使用現有的user_id列並更改它以使其可以爲空。完美的作品。

但是,當我運行migrate:rollback時,該列保持空值,並且存在不完美的向上/向下遷移關係。解決此問題的最佳實踐解決方案是什麼?

回答

2

我建議這樣做的唯一方法是運行DB::statement()方法。類似以下內容

public function down() { 
    DB::statement('ALTER TABLE `orders` MODIFY `user_id` INTEGER UNSIGNED NOT NULL;'); 
}