2016-05-04 65 views
0

如何正確更改/更新我的遷移,我正在使用Laravel 5.1。Laravel遷移更改/更新

的文件說,我應該用變化來更新列如:

$table->string('color', 10)->change(); 

但在那裏我必須把它的遷移和什麼命令我必須使用更新只是一個簡單的PHP工匠遷移?

我想這個至今:

public function up() 
{ 
    Schema::create('products', function (Blueprint $table)) { 
     $table->string('color', 5); 
    }); 

    Schema::table('products', function (Blueprint $table)) { 
     $table->string('color', 10)->change(); 
    }); 
} 

而且使用這個命令:

php artisan migrate 

但它並沒有改變。

+0

您可以使用'artisan'爲您生成一個遷移文件'php artisan make:migration yourmigrationname' – Tuim

回答

2

首先,創建新遷移以更改現有表列。

那麼你應該在你的新移民做這個裏面up()方法:

Schema::table('products', function (Blueprint $table)) { 
    $table->string('color', 10)->change(); 
}); 

所以,你有兩個遷移。首先創建了products表,其錯誤長度爲color字符串。

第二個遷移,將改變從5 color字符串長度至10

創建秒遷移後,再次運行php artisan migrate和變更將適用於表。

+0

什麼是遷移的好名字? –

+0

因此,我應該讓該新遷移的關閉方法爲空 –

+0

在執行此語法後,出現錯誤,意外的'>',期待'<' –

0

只是想我總結了我做了什麼來達到這個目的,來詳細說明上面的答案。

php artisan make:migration change_yourcolumn_to_float_in_yourtable 

然後,遷移的內部,在up()方法,我補上一句如下:

Schema::table('yourtable', function (Blueprint $table) { 
    $table->float('yourcolumn')->change(); // Changing from int to float, ie. 
}); 

那麼你可能需要添加一個包,以使這樣的數據庫修改: 進入控制檯:composer require doctrine/dbal

然後,您可以繼續並php artisan migrate提交更改。