實作前

實作中

產生 Migration 檔案

php artisan make:migration {action}_{table}_table

例如: php artisan make:migration create_tasks_table

上述範例為建立資料表語法,如果要在現有資料表中增加欄位,則需在後面加上「—table=(資料表)」。以下程式碼執行後會產生在「users」資料表中增加「facebook_id」欄位的 Migration 檔案。

php artisan make:migration add_facebook_id_to_users_table --table=users

編輯 Migration 檔案

檔案中定義了兩個方法:up 方法定義建立資料表、指定資料欄位類型、預設值(含外鍵設定)等作業;down 方法定義回復到未執行 up 方法前的作業內容(刪除資料表,含外鍵取消)。

public function up()
{
     Schema::create('tasks', function (Blueprint $table) {
         $table->id();
         $table->string('title',100);
         $table->integer('salary')->default(0);
         $table->text('desc')->nullable();
         $table->boolean('enabled')->default(true);
         $table->timestamps();
     });
}

public function down()
{
     Schema::dropIfExists('tasks');
}

執行 Migrate

執行 Migrate 作業

php artisan migrate

返回上一個階段

php artisan migrate:rollback

回到版本起點

php artisan migrate:reset

回到版本起點再到終點