2015-05-16 91 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;'); 
}