高影響變更 (High Impact Changes)
中等影響變更 (Medium Impact Changes)
低影響變更 (Low Impact Changes)
從 11.x 升級到 12.0 (Upgrade 12.0)
預估升級時間:5 分鐘
[!NOTE] 我們嘗試記錄每個可能的破壞性變更。由於這些破壞性變更有些在 framework 的隱蔽部分,只有一部分這些變更可能實際影響你的應用程式。想要節省時間嗎?你可以使用 Laravel Shift 來協助自動化你的應用程式升級。
更新依賴套件 (Updating Dependencies)
影響可能性:高
你應該在應用程式的 composer.json 檔案中更新以下依賴套件:
laravel/framework更新到^12.0phpunit/phpunit更新到^11.0pestphp/pest更新到^3.0
Carbon 3
影響可能性:低
已移除對 Carbon 2.x 的支援。所有 Laravel 12 應用程式現在需要 Carbon 3.x。
更新 Laravel 安裝器 (Updating The Laravel Installer)
如果你使用 Laravel 安裝器 CLI 工具來建立新的 Laravel 應用程式,你應該更新你的安裝器以與 Laravel 12.x 和新的 Laravel 入門套件相容。如果你透過 composer global require 安裝了 Laravel 安裝器,你可以使用 composer global update 來更新安裝器:
composer global update laravel/installer
如果你最初是透過 php.new 安裝 PHP 和 Laravel,你可以簡單地重新執行適用於你作業系統的 php.new 安裝命令,以安裝最新版本的 PHP 和 Laravel 安裝器:
/bin/bash -c "$(curl -fsSL https://php.new/install/mac/8.4)"
# 以管理員身分執行...
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://php.new/install/windows/8.4'))
/bin/bash -c "$(curl -fsSL https://php.new/install/linux/8.4)"
或者,如果你使用 Laravel Herd 內建的 Laravel 安裝器,你應該將 Herd 安裝更新到最新版本。
驗證 (Authentication)
更新 DatabaseTokenRepository 建構函式簽章 (Updated Databasetokenrepository Constructor Signature)
影響可能性:非常低
Illuminate\Auth\Passwords\DatabaseTokenRepository 類別的建構函式現在期望 $expires 參數以秒為單位,而不是分鐘。
Concurrency
Concurrency 結果索引映射 (Concurrency Result Index Mapping)
影響可能性:低
當使用關聯陣列呼叫 Concurrency::run 方法時,並行操作的結果現在會回傳關聯的鍵:
$result = Concurrency::run([
'task-1' => fn () => 1 + 1,
'task-2' => fn () => 2 + 2,
]);
// ['task-1' => 2, 'task-2' => 4]
Container
Container 類別依賴解析 (Container Class Dependency Resolution)
影響可能性:低
依賴注入容器現在在解析類別實例時會尊重類別屬性的預設值。如果你之前依賴容器在沒有預設值的情況下解析類別實例,你可能需要調整你的應用程式以適應這個新行為:
class Example
{
public function __construct(public ?Carbon $date = null) {}
}
$example = resolve(Example::class);
// <= 11.x
$example->date instanceof Carbon;
// >= 12.x
$example->date === null;
資料庫 (Database)
多 Schema 資料庫檢視 (Multi Schema Database Inspecting)
影響可能性:低
Schema::getTables()、Schema::getViews() 和 Schema::getTypes() 方法現在預設會包含所有 schemas 的結果。你可以傳遞 schema 參數來僅擷取指定 schema 的結果:
// 所有 schemas 上的所有資料表...
$tables = Schema::getTables();
// 'main' schema 上的所有資料表...
$tables = Schema::getTables(schema: 'main');
// 'main' 和 'blog' schemas 上的所有資料表...
$tables = Schema::getTables(schema: ['main', 'blog']);
Schema::getTableListing() 方法現在預設回傳帶有 schema 前綴的資料表名稱。你可以傳遞 schemaQualified 參數來依需要變更行為:
$tables = Schema::getTableListing();
// ['main.migrations', 'main.users', 'blog.posts']
$tables = Schema::getTableListing(schema: 'main');
// ['main.migrations', 'main.users']
$tables = Schema::getTableListing(schema: 'main', schemaQualified: false);
// ['migrations', 'users']
db:table 和 db:show 命令現在會在 MySQL、MariaDB 和 SQLite 上輸出所有 schemas 的結果,就像 PostgreSQL 和 SQL Server 一樣。
更新 Blueprint 建構函式簽章 (Updated Blueprint Constructor Signature)
影響可能性:非常低
Illuminate\Database\Schema\Blueprint 類別的建構函式現在期望 Illuminate\Database\Connection 的實例作為第一個參數。
Eloquent
Models 與 UUIDv7 (Models And Uuidv7)
影響可能性:中等
HasUuids trait 現在會回傳與 UUID 規範版本 7(有序 UUIDs)相容的 UUIDs。如果你想繼續為 model 的 IDs 使用有序 UUIDv4 字串,你現在應該使用 HasVersion4Uuids trait:
use Illuminate\Database\Eloquent\Concerns\HasUuids; // [tl! remove]
use Illuminate\Database\Eloquent\Concerns\HasVersion4Uuids as HasUuids; // [tl! add]
HasVersion7Uuids trait 已被移除。如果你之前使用這個 trait,你應該改用 HasUuids trait,它現在提供相同的行為。
請求 (Requests)
巢狀陣列請求合併 (Nested Array Request Merging)
影響可能性:低
$request->mergeIfMissing() 方法現在允許使用「點」標記法來合併巢狀陣列資料。如果你之前依賴此方法來建立包含「點」標記法版本的頂層陣列鍵,你可能需要調整你的應用程式以適應這個新行為:
$request->mergeIfMissing([
'user.last_name' => 'Otwell',
]);
儲存 (Storage)
本地檔案系統磁碟預設根路徑 (Local Filesystem Disk Default Root Path)
影響可能性:低
如果你的應用程式沒有在檔案系統設定中明確定義 local 磁碟,Laravel 現在會將本地磁碟的根目錄預設為 storage/app/private。在之前的版本中,這預設為 storage/app。因此,除非另有設定,否則呼叫 Storage::disk('local') 將從 storage/app/private 讀取和寫入。若要恢復之前的行為,你可以手動定義 local 磁碟並設定所需的根路徑。
驗證 (Validation)
Image 驗證現在排除 SVGs (Image Validation)
影響可能性:低
image 驗證規則現在預設不再允許 SVG 圖片。如果你想在使用 image 規則時允許 SVGs,你必須明確允許它們:
use Illuminate\Validation\Rules\File;
'photo' => 'required|image:allow_svg'
// 或者...
'photo' => ['required', File::image(allowSvg: true)],
其他 (Miscellaneous)
我們也鼓勵你檢視 laravel/laravel GitHub 儲存庫中的變更。雖然這些變更中有許多不是必需的,但你可能希望將這些檔案與你的應用程式保持同步。這些變更中的一些會在此升級指南中介紹,但其他的,例如設定檔或註解的變更,則不會。你可以使用 GitHub 比較工具輕鬆查看變更,並選擇哪些更新對你來說是重要的。