作為遷移的外鍵,請(qǐng)使用 unsignedInteger()
類型或 integer()->unsigned()
來(lái)替代 integer()
,否則你會(huì)得到 SQL 錯(cuò)誤。
Schema::create('employees', function (Blueprint $table) {
$table->unsignedInteger('company_id');
$table->foreign('company_id')->references('id')->on('companies');
// ...
});
同樣,你可以用 unsignedBigInteger()
如果外鍵對(duì)應(yīng)的是 bigInteger()
類型。
Schema::create('employees', function (Blueprint $table) {
$table->unsignedBigInteger('company_id');
});
如果你想改變數(shù)據(jù)庫(kù)遷移的順序,只需要將文件按時(shí)間戳記命名, 就像 2018_08_04_070443_create_posts_table.php
改為 2018_07_04_070443_create_posts_table.php
(從 2018_08_04
改成了
2018_07_04
).
遷移是以字母順序執(zhí)行。
你知不知道在遷移中不止有 timestamps()
還有帶時(shí)區(qū)的 timestampsTz()
。
Schema::create('employees', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->timestampsTz();
});
同樣,還有這么些列 dateTimeTz()
, timeTz()
, timestampTz()
, softDeletesTz()
。
遷移中有一些有趣的字段類型,下面是一些示例。
$table->geometry('positions');
$table->ipAddress('visitor');
$table->macAddress('device');
$table->point('position');
$table->uuid('id');
在 官方文檔 中你可以找到全部的字段類型列表。
當(dāng)創(chuàng)建遷移文件時(shí),你可以使用帶 useCurrent()
和 useCurrentOnUpdate()
可選項(xiàng)的 timestamp()
類型,這將會(huì)設(shè)置使相應(yīng)字段以 CURRENT_TIMESTAMP
作為默認(rèn)值。
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrentOnUpdate();
useCurrent()
會(huì)使用當(dāng)前時(shí)間,一般用作創(chuàng)建時(shí)間,且不會(huì)自動(dòng)更新;useCurrentOnUpdate()
將會(huì)使用當(dāng)前時(shí)間戳,并且在更新時(shí),自動(dòng)更新時(shí)間戳,一般用在 updated_at
字段。如果你想知道遷移是不是已經(jīng)運(yùn)行過(guò)了,不需要查看 “migrations” 表,你可以運(yùn)行 php artisan migrate:status
命令來(lái)查看。
結(jié)果示例:
+------+------------------------------------------------+-------+
| Ran? | Migration | Batch |
+------+------------------------------------------------+-------+
| Yes | 2014_10_12_000000_create_users_table | 1 |
| Yes | 2014_10_12_100000_create_password_resets_table | 1 |
| No | 2019_08_19_000000_create_failed_jobs_table | |
+------+------------------------------------------------+-------+
當(dāng)你打入 make:migration
命令,你不 “必須” 在不同部分間使用下劃線 _
進(jìn)行分隔,像是 create_transactions_table
。你可以把名字用引號(hào)引起來(lái)并把下劃線換成空格。
// 這樣可以工作
php artisan make:migration create_transactions_table
// 但這樣也可以
php artisan make:migration "create transactions table"
來(lái)源: Steve O 的 twitter
注意: 僅 MySQL 可用
如果你要在已經(jīng)存在的表里增加一個(gè)新列,這個(gè)列不 “必須” 成為最后一列,你可以指定這個(gè)列創(chuàng)建在哪個(gè)列的后面:
Schema::table('users', function (Blueprint $table) {
$table->string('phone')->after('email');
});
如果你要在已經(jīng)存在的表里增加一個(gè)新列,這個(gè)列不 “必須” 成為最后一列,你也可以指定這個(gè)列創(chuàng)建在哪個(gè)列的前面:
Schema::table('users', function (Blueprint $table) {
$table->string('phone')->before('created_at');
});
如果你讓創(chuàng)建的列排在表中的第一個(gè),那么可以使用 first
方法。
Schema::table('users', function (Blueprint $table) {
$table->string('uuid')->first();
});
如果你要為已經(jīng)存的表生成遷移文件,而且你想讓 Lavarel 來(lái)為你生成 Schema::table()
代碼,那么,在命令后面加入 in_xxxxx_table
,或者指明 --table
參數(shù)。php artisan change_fields_products_table
生成一個(gè)空類。
class ChangeFieldsProductsTable extends Migration
{
public function up()
{
//
}
}
但是,加入 in_xxxxx_table
php artisan make:migration
change_fields_in_products_table
生成了填好了 Schemma::table()
的類。
class ChangeFieldsProductsTable extends Migration
{
public function up()
{
Schema::table('products', function (Blueprint $table) {
//
})
};
}
同樣,你可以指明 --table
參數(shù) php artisan make:migration whatever_you_want --table=products
class WhateverYouWant extends Migration
{
public function up()
{
Schema::table('products', function (Blueprint $table) {
//
})
};
}
當(dāng)輸入 migrate --pretend
命令,你可以得到將在終端中執(zhí)行的 SQL 查詢。如果有需要的話調(diào)試 SQL 的方法,這是個(gè)很有趣的方法。
// Artisan 命令
php artisan migrate --pretend
更多建議: