資料庫:Migrations
簡介
Migrations 就像是資料庫的版本控制,讓您的團隊可以定義並共享應用程式的資料庫結構定義。如果您曾經必須在從版本控制拉取變更後,告訴同事手動在其本地資料庫結構中新增一個欄位,那麼您就遇到了資料庫 Migrations 所要解決的問題。
Laravel Schema Facade 提供了一種與資料庫無關的支援,用於在 Laravel 支援的所有資料庫系統中建立和操作資料表。通常,Migrations 會使用此 Facade 來建立和修改資料庫資料表與欄位。
生成 Migrations
您可以使用 make:migration Artisan 指令來生成資料庫 Migration。新的 Migration 將會被放置在您的 database/migrations 目錄中。每個 Migration 檔名都包含一個時間戳記,這讓 Laravel 可以確定 Migrations 的執行順序:
php artisan make:migration create_flights_tableLaravel 會根據 Migration 的名稱嘗試猜測資料表的名稱,以及該 Migration 是否要建立一個新資料表。如果 Laravel 能夠從 Migration 名稱中確定資料表名稱,則會在生成的 Migration 檔案中預填指定的資料表。否則,您只需手動在 Migration 檔案中指定資料表即可。
如果您想為生成的 Migration 指定自定義路徑,可以在執行 make:migration 指令時使用 --path 選項。給定的路徑應該相對於應用程式的根路徑。
📌 備註
Migration Stub 可以使用 Stub 發布進行自定義。
壓縮 Migrations
隨著您開發應用程式,隨著時間推移,您可能會累積越來越多的 Migrations。這可能會導致您的 database/migrations 目錄變得臃腫,甚至可能有數百個 Migrations。如果您願意,可以將 Migrations 「壓縮 (Squash)」成單個 SQL 檔案。要開始執行,請執行 schema:dump 指令:
php artisan schema:dump
# Dump the current database schema and prune all existing migrations...
php artisan schema:dump --prune當您執行此指令時,Laravel 會將一個「Schema」檔案寫入應用程式的 database/schema 目錄中。Schema 檔案的名稱將對應於資料庫連線。現在,當您嘗試遷移資料庫且沒有執行過其他 Migrations 時,Laravel 將首先執行您正在使用的資料庫連線之 Schema 檔案中的 SQL 語句。在執行 Schema 檔案的 SQL 語句後,Laravel 將執行不屬於 Schema Dump 的任何剩餘 Migrations。
如果您的應用程式測試使用的資料庫連線與您在本地開發時通常使用的不同,則應確保已使用該資料庫連線導出 Schema 檔案,以便您的測試能夠建立資料庫。您可能希望在導出平常在本地開發時使用的資料庫連線後執行此操作:
php artisan schema:dump
php artisan schema:dump --database=testing --prune您應該將資料庫 Schema 檔案提交到版本控制中,以便團隊中的其他新開發人員可以快速建立應用程式的初始資料庫結構。
⚠️ 警告
Migration 壓縮功能僅適用於 MariaDB、MySQL、PostgreSQL 和 SQLite 資料庫,並利用了資料庫的命令列用戶端。
Migration 結構
一個 Migration 類別包含兩個方法:up 和 down。up 方法用於向資料庫新增新的資料表、欄位或索引,而 down 方法則應該復原由 up 方法執行的操作。
在這兩個方法中,您都可以使用 Laravel Schema 構建器來直觀地建立和修改資料表。要瞭解 Schema 構建器上所有可用的方法,請查看其文件。例如,以下 Migration 建立了一個 flights 資料表:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('flights', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('airline');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::drop('flights');
}
};設定 Migration 連線
如果您的 Migration 將與應用程式預設資料庫連線以外的資料庫連線進行互動,您應該設定 Migration 的 $connection 屬性:
/**
* The database connection that should be used by the migration.
*
* @var string
*/
protected $connection = 'pgsql';
/**
* Run the migrations.
*/
public function up(): void
{
// ...
}跳過 Migrations
有時候,Migration 可能是為了支援尚未啟用的功能,而您現在還不希望它執行。在這種情況下,您可以在 Migration 中定義一個 shouldRun 方法。如果 shouldRun 方法回傳 false,則該 Migration 將被跳過:
use App\Models\Flight;
use Laravel\Pennant\Feature;
/**
* Determine if this migration should run.
*/
public function shouldRun(): bool
{
return Feature::active(Flight::class);
}執行 Migrations
若要執行所有待處理的 Migration,請執行 migrate Artisan 指令:
php artisan migrate如果您想查看哪些 Migration 已經執行、哪些仍在等待執行,您可以使用 migrate:status Artisan 指令:
php artisan migrate:status如果您想查看 Migration 將執行的 SQL 語句而不想實際執行它們,您可以在 migrate 指令中提供 --pretend 旗標:
php artisan migrate --pretend隔離 Migration 執行
如果您在多台伺服器上部署應用程式,並將執行 Migration 作為部署流程的一部分,您可能不希望兩台伺服器同時嘗試遷移資料庫。為了避免這種情況,您可以在呼叫 migrate 指令時使用 isolated 選項。
當提供 isolated 選項時,Laravel 會在嘗試執行 Migration 之前,使用應用程式的快取驅動程式取得一個原子鎖 (Atomic Lock)。在持有該鎖期間,所有其他執行 migrate 指令的嘗試都不會執行;然而,該指令仍會以成功的結束狀態碼退出:
php artisan migrate --isolated⚠️ 警告
若要使用此功能,您的應用程式必須使用 memcached、redis、dynamodb、database、file 或 array 作為應用程式預設的快取驅動程式。此外,所有伺服器都必須與同一個中央快取伺服器進行通訊。
在正式環境中強制執行 Migration
某些 Migration 操作是具有破壞性的,這意味著它們可能會導致資料遺失。為了防止您在正式環境資料庫上執行這些指令,在執行指令之前系統會提示您進行確認。若要強制指令在沒有提示的情況下執行,請使用 --force 旗標:
php artisan migrate --force回滾 Migrations
若要回滾最後一次的 Migration 操作,您可以使用 rollback Artisan 指令。此指令會回滾最後一「批 (Batch)」的 Migration,其中可能包含多個 Migration 檔案:
php artisan migrate:rollback您可以透過向 rollback 指令提供 step 選項來回滾特定數量的 Migration。例如,以下指令將回滾最後五個 Migration:
php artisan migrate:rollback --step=5您可以透過向 rollback 指令提供 batch 選項來回滾特定「批次」的 Migration,其中 batch 選項對應於應用程式 migrations 資料庫表中的批次值。例如,以下指令將回滾批次三中的所有 Migration:
php artisan migrate:rollback --batch=3如果您想查看 Migration 將執行的 SQL 語句而不想實際執行它們,您可以在 migrate:rollback 指令中使用 --pretend 旗標:
php artisan migrate:rollback --pretendmigrate:reset 指令將回滾應用程式所有的 Migration:
php artisan migrate:reset使用單一指令回滾並遷移
migrate:refresh 指令將回滾所有 Migration,然後執行 migrate 指令。此指令可有效地重新建立您的整個資料庫:
php artisan migrate:refresh
# Refresh the database and run all database seeds...
php artisan migrate:refresh --seed您可以透過向 refresh 指令提供 step 選項來回滾並重新遷移特定數量的 Migration。例如,以下指令將回滾並重新遷移最後五個 Migration:
php artisan migrate:refresh --step=5移除所有資料表並遷移
migrate:fresh 指令將從資料庫中移除所有資料表,然後執行 migrate 指令:
php artisan migrate:fresh
php artisan migrate:fresh --seed預設情況下,migrate:fresh 指令僅從預設資料庫連接中移除資料表。但是,您可以使用 --database 選項指定應遷移的資料庫連接。資料庫連接名稱應對應於應用程式 database 設定檔中定義的連接:
php artisan migrate:fresh --database=admin⚠️ 警告
migrate:fresh 指令將移除所有資料庫表,無論其前綴為何。在與其他應用程式共享的資料庫上進行開發時,應謹慎使用此指令。
資料表
建立資料表
若要建立新的資料表,請使用 Schema Facade 的 create 方法。create 方法接受兩個參數:第一個是資料表的名稱,第二個則是接收 Blueprint 物件的閉包 (Closure),可用於定義新資料表:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email');
$table->timestamps();
});建立資料表時,您可以使用 Schema 建構器的任何 欄位方法 來定義資料表的欄位。
判斷資料表 / 欄位是否存在
您可以使用 hasTable、hasColumn 與 hasIndex 方法來判斷資料表、欄位或索引是否存在:
if (Schema::hasTable('users')) {
// The "users" table exists...
}
if (Schema::hasColumn('users', 'email')) {
// The "users" table exists and has an "email" column...
}
if (Schema::hasIndex('users', ['email'], 'unique')) {
// The "users" table exists and has a unique index on the "email" column...
}資料庫連線與資料表選項
如果您想在非應用程式預設連線的資料庫連線執行 Schema 操作,請使用 connection 方法:
Schema::connection('sqlite')->create('users', function (Blueprint $table) {
$table->id();
});此外,還可以使用其他一些屬性和方法來定義資料表建立的其他面向。在使用 MariaDB 或 MySQL 時,可以使用 engine 屬性來指定資料表的儲存引擎:
Schema::create('users', function (Blueprint $table) {
$table->engine('InnoDB');
// ...
});在使用 MariaDB 或 MySQL 時,可以使用 charset 與 collation 屬性來指定所建立資料表的字元集與定序 (Collation):
Schema::create('users', function (Blueprint $table) {
$table->charset('utf8mb4');
$table->collation('utf8mb4_unicode_ci');
// ...
});temporary 方法可用於表示該資料表應該是「暫時性」的。暫時性資料表僅對當前連線的資料庫工作階段 (Session) 可見,並在連線關閉時自動刪除:
Schema::create('calculations', function (Blueprint $table) {
$table->temporary();
// ...
});如果您想為資料表加入「註解 (Comment)」,可以在資料表執行個體上呼叫 comment 方法。資料表註解目前僅由 MariaDB、MySQL 與 PostgreSQL 支援:
Schema::create('calculations', function (Blueprint $table) {
$table->comment('Business calculations');
// ...
});更新資料表
Schema Facade 的 table 方法可用於更新現有的資料表。與 create 方法類似,table 方法接受兩個參數:資料表名稱,以及一個接收 Blueprint 執行個體的閉包,您可以使用該執行個體向資料表新增欄位或索引:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Schema::table('users', function (Blueprint $table) {
$table->integer('votes');
});重新命名 / 移除資料表
若要重新命名現有的資料表,請使用 rename 方法:
use Illuminate\Support\Facades\Schema;
Schema::rename($from, $to);若要移除現有的資料表,您可以使用 drop 或 dropIfExists 方法:
Schema::drop('users');
Schema::dropIfExists('users');重新命名帶有外鍵的資料表
在重新命名資料表之前,您應該確認資料表上的任何外鍵約束在 Migration 檔案中都有明確的名稱,而不是讓 Laravel 指定基於慣例的名稱。否則,外鍵約束名稱將會指向舊的資料表名稱。
欄位
建立欄位
Schema Facade 上的 table 方法可用於更新現有的資料表。就像 create 方法一樣,table 方法接受兩個參數:資料表的名稱,以及一個接收 Illuminate\Database\Schema\Blueprint 實例的閉包 (Closure),您可以使用它來向資料表新增欄位:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Schema::table('users', function (Blueprint $table) {
$table->integer('votes');
});可用的欄位類型
Schema 構建器 Blueprint 提供了多種方法,對應到您可以新增到資料表中的不同欄位類型。下表列出了所有可用的方法:
布林類型
字串與文字類型
數值類型
bigIncrementsbigIntegerdecimaldoublefloatidincrementsintegermediumIncrementsmediumIntegersmallIncrementssmallIntegertinyIncrementstinyIntegerunsignedBigIntegerunsignedIntegerunsignedMediumIntegerunsignedSmallIntegerunsignedTinyInteger
日期與時間類型
二進位類型
物件與 JSON 類型
UUID 與 ULID 類型
空間類型
關聯類型
特殊類型
bigIncrements()
bigIncrements 方法建立一個自動遞增且為 UNSIGNED BIGINT (主鍵) 的等效欄位:
$table->bigIncrements('id');bigInteger()
bigInteger 方法建立一個 BIGINT 的等效欄位:
$table->bigInteger('votes');binary()
binary 方法建立一個 BLOB 的等效欄位:
$table->binary('photo');使用 MySQL、MariaDB 或 SQL Server 時,您可以傳遞 length 與 fixed 參數來建立 VARBINARY 或 BINARY 的等效欄位:
$table->binary('data', length: 16); // VARBINARY(16)
$table->binary('data', length: 16, fixed: true); // BINARY(16)boolean()
boolean 方法建立一個 BOOLEAN 的等效欄位:
$table->boolean('confirmed');char()
char 方法建立一個指定長度的 CHAR 等效欄位:
$table->char('name', length: 100);dateTimeTz()
dateTimeTz 方法建立一個 DATETIME (含時區) 的等效欄位,並可選擇性指定小數秒數精度:
$table->dateTimeTz('created_at', precision: 0);dateTime()
dateTime 方法建立一個 DATETIME 的等效欄位,並可選擇性指定小數秒數精度:
$table->dateTime('created_at', precision: 0);date()
date 方法建立一個 DATE 的等效欄位:
$table->date('created_at');decimal()
decimal 方法建立一個具備指定精度 (總位數) 與標度 (小數位數) 的 DECIMAL 等效欄位:
$table->decimal('amount', total: 8, places: 2);double()
double 方法建立一個 DOUBLE 的等效欄位:
$table->double('amount');enum()
enum 方法建立一個具備指定有效值列表的 ENUM 等效欄位:
$table->enum('difficulty', ['easy', 'hard']);當然,您可以使用 Enum::cases() 方法,而不是手動定義一個包含允許值的陣列:
use App\Enums\Difficulty;
$table->enum('difficulty', Difficulty::cases());float()
float 方法建立一個指定精度的 FLOAT 等效欄位:
$table->float('amount', precision: 53);foreignId()
foreignId 方法建立一個 UNSIGNED BIGINT 的等效欄位:
$table->foreignId('user_id');foreignIdFor()
foreignIdFor 方法為給定的模型類別新增一個 {column}_id 等效欄位。欄位類型將根據模型的主鍵類型而定,可能是 UNSIGNED BIGINT、CHAR(36) 或 CHAR(26):
$table->foreignIdFor(User::class);foreignUlid()
foreignUlid 方法建立一個 ULID 的等效欄位:
$table->foreignUlid('user_id');foreignUuid()
foreignUuid 方法建立一個 UUID 的等效欄位:
$table->foreignUuid('user_id');geography()
geography 方法建立一個具備指定空間類型與 SRID (空間參考系統識別碼) 的 GEOGRAPHY 等效欄位:
$table->geography('coordinates', subtype: 'point', srid: 4326);📌 備註
對空間類型的支援取決於您的資料庫驅動程式。請參閱您的資料庫文件。如果您的應用程式使用的是 PostgreSQL 資料庫,則必須在選用 geography 方法之前安裝 PostGIS 擴充功能。
geometry()
geometry 方法建立一個具備指定空間類型與 SRID (空間參考系統識別碼) 的 GEOMETRY 等效欄位:
$table->geometry('positions', subtype: 'point', srid: 0);📌 備註
對空間類型的支援取決於您的資料庫驅動程式。請參閱您的資料庫文件。如果您的應用程式使用的是 PostgreSQL 資料庫,則必須在選用 geometry 方法之前安裝 PostGIS 擴充功能。
id()
id 方法是 bigIncrements 方法的別名。預設情況下,該方法將建立一個 id 欄位;但是,如果您想為該欄位指定不同的名稱,可以傳遞一個欄位名稱:
$table->id();increments()
increments 方法建立一個自動遞增且為 UNSIGNED INTEGER (主鍵) 的等效欄位:
$table->increments('id');integer()
integer 方法建立一個 INTEGER 的等效欄位:
$table->integer('votes');ipAddress()
ipAddress 方法建立一個 VARCHAR 的等效欄位:
$table->ipAddress('visitor');當使用 PostgreSQL 時,將建立一個 INET 欄位。
json()
json 方法建立一個 JSON 的等效欄位:
$table->json('options');當使用 SQLite 時,將建立一個 TEXT 欄位。
jsonb()
jsonb 方法建立一個 JSONB 的等效欄位:
$table->jsonb('options');當使用 SQLite 時,將建立一個 TEXT 欄位。
longText()
longText 方法建立一個 LONGTEXT 的等效欄位:
$table->longText('description');使用 MySQL 或 MariaDB 時,您可以對該欄位套用 binary 字元集,以建立一個 LONGBLOB 等效欄位:
$table->longText('data')->charset('binary'); // LONGBLOBmacAddress()
macAddress 方法建立一個旨在保存 MAC 位址的欄位。某些資料庫系統(例如 PostgreSQL)有專門用於此類資料的欄位類型。其他資料庫系統則會使用字串等效欄位:
$table->macAddress('device');mediumIncrements()
mediumIncrements 方法建立一個自動遞增且為 UNSIGNED MEDIUMINT (主鍵) 的等效欄位:
$table->mediumIncrements('id');mediumInteger()
mediumInteger 方法建立一個 MEDIUMINT 的等效欄位:
$table->mediumInteger('votes');mediumText()
mediumText 方法建立一個 MEDIUMTEXT 的等效欄位:
$table->mediumText('description');使用 MySQL 或 MariaDB 時,您可以對該欄位套用 binary 字元集,以建立一個 MEDIUMBLOB 等效欄位:
$table->mediumText('data')->charset('binary'); // MEDIUMBLOBmorphs()
morphs 方法是一個便捷方法,它會新增一個 {column}_id 等效欄位和一個 {column}_type 的 VARCHAR 等效欄位。{column}_id 的欄位類型將取決於模型的主鍵類型,可能是 UNSIGNED BIGINT、CHAR(36) 或 CHAR(26)。
此方法旨在定義多型 Eloquent 關聯所需的欄位。在以下範例中,將建立 taggable_id 和 taggable_type 欄位:
$table->morphs('taggable');nullableMorphs()
該方法與 morphs 方法類似;但是,建立的欄位將會是「可為空 (nullable)」的:
$table->nullableMorphs('taggable');nullableUlidMorphs()
該方法與 ulidMorphs 方法類似;但是,建立的欄位將會是「可為空 (nullable)」的:
$table->nullableUlidMorphs('taggable');nullableUuidMorphs()
該方法與 uuidMorphs 方法類似;但是,建立的欄位將會是「可為空 (nullable)」的:
$table->nullableUuidMorphs('taggable');rememberToken()
rememberToken 方法建立一個可為空且為 VARCHAR(100) 的等效欄位,該欄位旨在儲存目前的「記住我」身分驗證權杖:
$table->rememberToken();set()
set 方法建立一個具備指定有效值列表的 SET 等效欄位:
$table->set('flavors', ['strawberry', 'vanilla']);smallIncrements()
smallIncrements 方法建立一個自動遞增且為 UNSIGNED SMALLINT (主鍵) 的等效欄位:
$table->smallIncrements('id');smallInteger()
smallInteger 方法建立一個 SMALLINT 的等效欄位:
$table->smallInteger('votes');softDeletesTz()
softDeletesTz 方法新增一個可為空且為 TIMESTAMP (含時區) 的 deleted_at 等效欄位,並可選擇性指定小數秒數精度。此欄位旨在儲存 Eloquent「軟刪除」功能所需的 deleted_at 時間戳記:
$table->softDeletesTz('deleted_at', precision: 0);softDeletes()
softDeletes 方法新增一個可為空且為 TIMESTAMP 的 deleted_at 等效欄位,並可選擇性指定小數秒數精度。此欄位旨在儲存 Eloquent「軟刪除」功能所需的 deleted_at 時間戳記:
$table->softDeletes('deleted_at', precision: 0);string()
string 方法建立一個指定長度的 VARCHAR 等效欄位:
$table->string('name', length: 100);text()
text 方法建立一個 TEXT 的等效欄位:
$table->text('description');使用 MySQL 或 MariaDB 時,您可以對該欄位套用 binary 字元集,以建立一個 BLOB 等效欄位:
$table->text('data')->charset('binary'); // BLOBtimeTz()
timeTz 方法建立一個 TIME (含時區) 的等效欄位,並可選擇性指定小數秒數精度:
$table->timeTz('sunrise', precision: 0);time()
time 方法建立一個 TIME 的等效欄位,並可選擇性指定小數秒數精度:
$table->time('sunrise', precision: 0);timestampTz()
timestampTz 方法建立一個 TIMESTAMP (含時區) 的等效欄位,並可選擇性指定小數秒數精度:
$table->timestampTz('added_at', precision: 0);timestamp()
timestamp 方法建立一個 TIMESTAMP 的等效欄位,並可選擇性指定小數秒數精度:
$table->timestamp('added_at', precision: 0);timestampsTz()
timestampsTz 方法建立 created_at 與 updated_at 的 TIMESTAMP (含時區) 等效欄位,並可選擇性指定小數秒數精度:
$table->timestampsTz(precision: 0);timestamps()
timestamps 方法建立 created_at 與 updated_at 的 TIMESTAMP 等效欄位,並可選擇性指定小數秒數精度:
$table->timestamps(precision: 0);tinyIncrements()
tinyIncrements 方法建立一個自動遞增且為 UNSIGNED TINYINT (主鍵) 的等效欄位:
$table->tinyIncrements('id');tinyInteger()
tinyInteger 方法建立一個 TINYINT 的等效欄位:
$table->tinyInteger('votes');tinyText()
tinyText 方法建立一個 TINYTEXT 的等效欄位:
$table->tinyText('notes');使用 MySQL 或 MariaDB 時,您可以對該欄位套用 binary 字元集,以建立一個 TINYBLOB 等效欄位:
$table->tinyText('data')->charset('binary'); // TINYBLOBunsignedBigInteger()
unsignedBigInteger 方法建立一個 UNSIGNED BIGINT 的等效欄位:
$table->unsignedBigInteger('votes');unsignedInteger()
unsignedInteger 方法建立一個 UNSIGNED INTEGER 的等效欄位:
$table->unsignedInteger('votes');unsignedMediumInteger()
unsignedMediumInteger 方法建立一個 UNSIGNED MEDIUMINT 的等效欄位:
$table->unsignedMediumInteger('votes');unsignedSmallInteger()
unsignedSmallInteger 方法建立一個 UNSIGNED SMALLINT 的等效欄位:
$table->unsignedSmallInteger('votes');unsignedTinyInteger()
unsignedTinyInteger 方法建立一個 UNSIGNED TINYINT 的等效欄位:
$table->unsignedTinyInteger('votes');ulidMorphs()
ulidMorphs 方法是一個便捷方法,它會新增一個 {column}_id 的 CHAR(26) 等效欄位和一個 {column}_type 的 VARCHAR 等效欄位。
此方法旨在定義使用 ULID 識別碼的多型 Eloquent 關聯所需的欄位。在以下範例中,將建立 taggable_id 和 taggable_type 欄位:
$table->ulidMorphs('taggable');uuidMorphs()
uuidMorphs 方法是一個便捷方法,它會新增一個 {column}_id 的 CHAR(36) 等效欄位和一個 {column}_type 的 VARCHAR 等效欄位。
此方法旨在定義使用 UUID 識別碼的多型 Eloquent 關聯所需的欄位。在以下範例中,將建立 taggable_id 和 taggable_type 欄位:
$table->uuidMorphs('taggable');ulid()
ulid 方法建立一個 ULID 的等效欄位:
$table->ulid('id');uuid()
uuid 方法建立一個 UUID 的等效欄位:
$table->uuid('id');vector()
vector 方法建立一個 vector 的等效欄位:
$table->vector('embedding', dimensions: 100);year()
year 方法建立一個 YEAR 的等效欄位:
$table->year('birth_year');欄位修飾詞
除了上方列出的欄位類型外,在將欄位加入資料庫資料表時,還有多種欄位「修飾詞」可供使用。例如,若要讓欄位「可為空 (nullable)」,您可以使用 nullable 方法:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Schema::table('users', function (Blueprint $table) {
$table->string('email')->nullable();
});下表包含所有可用的欄位修飾詞。此列表不包含 索引修飾詞:
| 修飾詞 | 說明 |
|---|---|
->after('column') | 將欄位放置於另一個欄位「之後」(MariaDB / MySQL)。 |
->autoIncrement() | 將 INTEGER 欄位設定為自動遞增 (主鍵)。 |
->charset('utf8mb4') | 為欄位指定字元集 (MariaDB / MySQL)。 |
->collation('utf8mb4_unicode_ci') | 為欄位指定排序規則。 |
->comment('my comment') | 為欄位加上註解 (MariaDB / MySQL / PostgreSQL)。 |
->default($value) | 為欄位指定「預設值」。 |
->first() | 將欄位放置在資料表的「首位」(MariaDB / MySQL)。 |
->from($integer) | 設定自動遞增欄位的起始值 (MariaDB / MySQL / PostgreSQL)。 |
->invisible() | 對 SELECT * 查詢而言,使欄位變為「隱形」(MariaDB / MySQL)。 |
->nullable($value = true) | 允許將 NULL 值插入欄位。 |
->storedAs($expression) | 建立一個儲存型產生的欄位 (MariaDB / MySQL / PostgreSQL / SQLite)。 |
->unsigned() | 將 INTEGER 欄位設定為 UNSIGNED (MariaDB / MySQL)。 |
->useCurrent() | 設定 TIMESTAMP 欄位預設使用 CURRENT_TIMESTAMP。 |
->useCurrentOnUpdate() | 設定 TIMESTAMP 欄位在紀錄更新時使用 CURRENT_TIMESTAMP (MariaDB / MySQL)。 |
->virtualAs($expression) | 建立一個虛擬型產生的欄位 (MariaDB / MySQL / SQLite)。 |
->generatedAs($expression) | 使用指定的序列選項建立一個識別欄位 (Identity column) (PostgreSQL)。 |
->always() | 為識別欄位定義序列值優於輸入值的權限 (PostgreSQL)。 |
預設值運算式
default 修飾詞接受一個值或一個 Illuminate\Database\Query\Expression 實例。使用 Expression 實例可以防止 Laravel 將該值包在引號中,並允許您使用資料庫特定的函數。這在您需要為 JSON 欄位分配預設值時特別有用:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Migrations\Migration;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('flights', function (Blueprint $table) {
$table->id();
$table->json('movies')->default(new Expression('(JSON_ARRAY())'));
$table->timestamps();
});
}
};⚠️ 警告
對預設值運算式的支援取決於您的資料庫驅動程式、資料庫版本以及欄位類型。請參考您所使用的資料庫文件。
欄位順序
使用 MariaDB 或 MySQL 資料庫時,可以使用 after 方法將欄位加入 Schema 中現有欄位的後方:
$table->after('password', function (Blueprint $table) {
$table->string('address_line1');
$table->string('address_line2');
$table->string('city');
});修改欄位
change 方法允許您修改現有欄位的類型與屬性。例如,您可能希望增加 string 欄位的大小。為了展示 change 方法,讓我們將 name 欄位的大小從 25 增加到 50。要達成此目的,我們只需定義欄位的新狀態,然後呼叫 change 方法:
Schema::table('users', function (Blueprint $table) {
$table->string('name', 50)->change();
});修改欄位時,您必須明確地包含所有您想要保留在欄位定義中的修飾詞 —— 任何缺失的屬性都將被移除。例如,為了保留 unsigned、default 與 comment 屬性,您必須在修改欄位時明確呼叫每個修飾詞:
Schema::table('users', function (Blueprint $table) {
$table->integer('votes')->unsigned()->default(1)->comment('my comment')->change();
});change 方法不會更改欄位的索引。因此,在修改欄位時,您可以使用索引修飾詞來明確地新增或移除索引:
// Add an index...
$table->bigIncrements('id')->primary()->change();
// Drop an index...
$table->char('postal_code', 10)->unique(false)->change();重新命名欄位
若要重新命名欄位,您可以使用 Schema 建構器提供的 renameColumn 方法:
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('from', 'to');
});移除欄位
若要移除欄位,您可以使用 Schema 建構器上的 dropColumn 方法:
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('votes');
});您可以藉由傳遞一個欄位名稱陣列給 dropColumn 方法,從資料表中移除多個欄位:
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['votes', 'avatar', 'location']);
});可用的指令別名
Laravel 提供了幾個方便的方法,用於移除常見類型的欄位。下表說明了其中的每一個方法:
| 指令 | 說明 |
|---|---|
$table->dropMorphs('morphable'); | 移除 morphable_id 與 morphable_type 欄位。 |
$table->dropRememberToken(); | 移除 remember_token 欄位。 |
$table->dropSoftDeletes(); | 移除 deleted_at 欄位。 |
$table->dropSoftDeletesTz(); | dropSoftDeletes() 方法的別名。 |
$table->dropTimestamps(); | 移除 created_at 與 updated_at 欄位。 |
$table->dropTimestampsTz(); | dropTimestamps() 方法的別名。 |
索引
建立索引
Laravel 的結構產生器支援多種索引類型。以下範例建立了一個新的 email 欄位,並指定其值必須是唯一的。若要建立索引,我們可以將 unique 方法鏈結到欄位定義上:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Schema::table('users', function (Blueprint $table) {
$table->string('email')->unique();
});或者,您可以在定義欄位後再建立索引。若要這麼做,您應該在結構產生器藍圖上呼叫 unique 方法。此方法接受應接收唯一索引的欄位名稱:
$table->unique('email');您甚至可以向索引方法傳遞一個欄位陣列,以建立複合(或組合)索引:
$table->index(['account_id', 'created_at']);建立索引時,Laravel 會根據資料表、欄位名稱和索引類型自動產生索引名稱,但您可以向該方法傳遞第二個參數,以自行指定索引名稱:
$table->unique('email', 'unique_email');可用的索引類型
Laravel 的結構產生器藍圖類別提供了建立 Laravel 支援的每種索引類型的方法。每個索引方法都接受一個選用的第二個參數來指定索引名稱。如果省略,名稱將根據用於索引的資料表和欄位名稱以及索引類型衍生而來。下表說明了每個可用的索引方法:
| 指令 | 描述 |
|---|---|
$table->primary('id'); | 新增一個主鍵。 |
$table->primary(['id', 'parent_id']); | 新增複合鍵。 |
$table->unique('email'); | 新增一個唯一索引。 |
$table->index('state'); | 新增一個索引。 |
$table->fullText('body'); | 新增一個全文索引 (MariaDB / MySQL / PostgreSQL)。 |
$table->fullText('body')->language('english'); | 新增一個指定語言的全文索引 (PostgreSQL)。 |
$table->spatialIndex('location'); | 新增一個空間索引 (除 SQLite 外)。 |
重新命名索引
若要重新命名索引,您可以使用結構產生器藍圖提供的 renameIndex 方法。此方法接受目前的索引名稱作為其第一個參數,並將所需的名稱作為其第二個參數:
$table->renameIndex('from', 'to')移除索引
若要移除索引,您必須指定索引的名稱。預設情況下,Laravel 會根據資料表名稱、索引欄位的名稱和索引類型自動分配索引名稱。以下是一些範例:
| 指令 | 描述 |
|---|---|
$table->dropPrimary('users_id_primary'); | 從 "users" 資料表中移除主鍵。 |
$table->dropUnique('users_email_unique'); | 從 "users" 資料表中移除唯一索引。 |
$table->dropIndex('geo_state_index'); | 從 "geo" 資料表中移除基本索引。 |
$table->dropFullText('posts_body_fulltext'); | 從 "posts" 資料表中移除全文索引。 |
$table->dropSpatialIndex('geo_location_spatialindex'); | 從 "geo" 資料表中移除空間索引 (除 SQLite 外)。 |
如果您將欄位陣列傳遞給移除索引的方法,則會根據資料表名稱、欄位和索引類型產生慣例的索引名稱:
Schema::table('geo', function (Blueprint $table) {
$table->dropIndex(['state']); // Drops index 'geo_state_index'
});外鍵約束
Laravel 也提供建立外鍵約束的支持,用於在資料庫層級強制執行參照完整性。例如,讓我們在 posts 資料表上定義一個 user_id 欄位,它參照 users 資料表的 id 欄位:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Schema::table('posts', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});由於此語法相當冗長,Laravel 提供了額外的、更簡潔的方法,利用慣例來提供更好的開發體驗。當使用 foreignId 方法建立欄位時,上述範例可以改寫如下:
Schema::table('posts', function (Blueprint $table) {
$table->foreignId('user_id')->constrained();
});foreignId 方法會建立一個等同於 UNSIGNED BIGINT 的欄位,而 constrained 方法將使用慣例來確定所參照的資料表和欄位。如果您的資料表名稱不符合 Laravel 的慣例,您可以手動將其提供給 constrained 方法。此外,也可以指定應分配給生成的索引的名稱:
Schema::table('posts', function (Blueprint $table) {
$table->foreignId('user_id')->constrained(
table: 'users', indexName: 'posts_user_id'
);
});您還可以為約束的 "on delete" 和 "on update" 屬性指定所需的動作:
$table->foreignId('user_id')
->constrained()
->onUpdate('cascade')
->onDelete('cascade');這些動作也提供了另一種具表達力的語法:
| 方法 | 描述 |
|---|---|
$table->cascadeOnUpdate(); | 更新時應連動 (Cascade)。 |
$table->restrictOnUpdate(); | 更新時應受限制。 |
$table->nullOnUpdate(); | 更新時應將外鍵值設為 null。 |
$table->noActionOnUpdate(); | 更新時不採取動作。 |
$table->cascadeOnDelete(); | 刪除時應連動 (Cascade)。 |
$table->restrictOnDelete(); | 刪除時應受限制。 |
$table->nullOnDelete(); | 刪除時應將外鍵值設為 null。 |
$table->noActionOnDelete(); | 如果子紀錄存在,則防止刪除。 |
任何額外的欄位修飾詞都必須在 constrained 方法之前呼叫:
$table->foreignId('user_id')
->nullable()
->constrained();移除外鍵
要移除外鍵,您可以使用 dropForeign 方法,並將要刪除的外鍵約束名稱作為參數傳遞。外鍵約束使用與索引相同的命名慣例。換句話說,外鍵約束名稱是基於資料表名稱和約束中的欄位名稱,後接 "_foreign" 字尾:
$table->dropForeign('posts_user_id_foreign');或者,您可以將包含持有外鍵的欄位名稱的陣列傳遞給 dropForeign 方法。該陣列將根據 Laravel 的約束命名慣例轉換為外鍵約束名稱:
$table->dropForeign(['user_id']);切換外鍵約束
您可以在 migration 中使用以下方法啟用或停用外鍵約束:
Schema::enableForeignKeyConstraints();
Schema::disableForeignKeyConstraints();
Schema::withoutForeignKeyConstraints(function () {
// Constraints disabled within this closure...
});⚠️ 警告
SQLite 預設會停用外鍵約束。使用 SQLite 時,請確保在嘗試於 migration 中建立外鍵之前,已在資料庫設定中啟用外鍵支援。
事件
為了方便起見,每個 Migration 操作都會分派一個 事件。以下所有事件都繼承了基礎的 Illuminate\Database\Events\MigrationEvent 類別:
| Class | Description |
|---|---|
Illuminate\Database\Events\MigrationsStarted | 一整批 Migration 即將執行。 |
Illuminate\Database\Events\MigrationsEnded | 一整批 Migration 已執行完畢。 |
Illuminate\Database\Events\MigrationStarted | 單個 Migration 即將執行。 |
Illuminate\Database\Events\MigrationEnded | 單個 Migration 已執行完畢。 |
Illuminate\Database\Events\NoPendingMigrations | Migration 命令找不到待處理的 Migration。 |
Illuminate\Database\Events\SchemaDumped | 資料庫結構傾印 (Dump) 已完成。 |
Illuminate\Database\Events\SchemaLoaded | 已載入現有的資料庫結構傾印 (Dump)。 |