close

一個 Migrate 會包含兩個方法:up 和 down。
up 方法用於在資料庫內增加新的資料表表、欄位、或索引,而 down 方法則必須簡單的反向執行 up 方法的操作。

 

Migrate 基本語法

php artisan  // 會顯示所有的命令列

php artisan migrate  // 執行你應用程式中所有未完成的 Migrate

php artisan migrate:rollback    // 返回前一版本的 database

php artisan migrate:rollback --step=5  // 返回前五版本的 database

php artisan help make:migration    // 顯示還可以使用那些參數

php artisan migrate:reset   // 指令會還原應用程式的所有遷移

php artisan migrate:refresh   // 指令首先會還原你資料庫的所有 Migrate,接著再執行 Migrate 指令。此指令能有效的重新建立整個資料庫

php artisan migrate:refresh --step=5  // 還原你資料庫的前五版本 Migrate,接著再執行前五版本 Migrate 指令。

php artisan migrate --force  // 要忽略安全提示強制執行指令,你可以使用 --force 標記

 

資料表

新增

使用 --create 參數

php artisan make:migration create_users_table --create=users
Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
});

指定

(已有資料表) 使用 --table 參數 , 若不加 --table 參數在 up & down function 中會沒有內容

php artisan make:migration add_votes_to_users_table --table=users

重新命名

若要重新命名一張已存在的資料表,可以使用 rename 方法:

Schema::rename($from, $to);

刪除

要刪除已存在的資料表,你可使用 drop  dropIfExists 方法:

Schema::drop('users');

Schema::dropIfExists('users');

檢查資料表是否存在

if (Schema::hasTable('users')) {
    //
}

 

資料表欄位

變更資料表欄位前必須在你的 composer.json 的增加 doctrine/dbal 依賴。

意思就是安裝套件, 指令: composer require doctrine/dbal

 

新增與刪除欄位

有兩種時間點

1. 資料表正要新建, up 時新增資料表與欄位, down 時刪除資料表

    public function up() {
        Schema::create('flights', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

    public function down() {
        Schema::drop('flights');
    }

2. 在已有 table 做欄位修改, up 時新加資料欄位, down 時刪除欄位

public function up() { 
  Schema::table('users', function ($table) {
    $table->string('email');
  });
}

public function down() { 
  Schema::table('users', function ($table) {
    $table->dropColumn('email');  
  });
}


欄位修飾

有一些其它的欄位「修飾」,你可以將它增加至欄位。
例如,若要讓欄位「nullable」(可空值) ,那麼你可以使用 nullable 方法:

Schema::table('users', function ($table) {
    $table->string('email')->nullable();
});


更新欄位屬性
change 方法讓你可以修改一個已存在欄位的類型,或修改欄位的屬性。
例如,你可以想增加字串欄位的長度。要看看  change 方法的作用,讓我們將 name 欄位的長度從 25 增加到 50:

Schema::table('users', function ($table) {
    $table->string('name', 50)->change();
});

我們也能將欄位修改為 nullable:

Schema::table('users', function ($table) {
    $table->string('name', 50)->nullable()->change();
});

 

重新命名欄位名稱

你可使用結構建構器的 renameColumn 方法。

Schema::table('users', function ($table) {
    $table->renameColumn('from', 'to');
});

 

移除欄位

可使用結構建構器的 dropColumn 方法 (移除單筆欄位):

Schema::table('users', function ($table) {
    $table->dropColumn('votes');
});

你可以傳遞欄位名稱的陣列至 dropCloumn 方法 (移除多筆欄位):

Schema::table('users', function ($table) {
    $table->dropColumn(['votes', 'avatar', 'location']);
});

 

檢查資料表欄位是否存在

if (Schema::hasColumn('users', 'email')) {
    //
}

 

連接非預設的資料庫進行結構操作

連結到 foo 資料庫 , 新增 user 資料表

Schema::connection('foo')->create('users', function ($table) {
    $table->increments('id');
});

 

※ 當使用 SQLite 資料庫時並不支援在單行遷移中移除或修改多筆欄位。

※ 如果在你執行 php artisan migrate 時出現「class not found」的錯誤,請試著在執行 composer dump-autoload 指令後再次執行一次遷移指令。

 

資料來源:https://laravel.tw/docs/5.2/migrations

arrow
arrow
    全站熱搜

    Mayuge 發表在 痞客邦 留言(0) 人氣()