Ray Lee | 李宗叡
Learn or Die
Published in
3 min readJul 19, 2024

--

# 前言

紀錄由 Laravel 10 升級到 Laravel 11 的步驟記錄

# 步驟

# 詳閱文件

official document guide

# 修改 composer.json

根據官方文件,修改以下的 package version

  • laravel/framework to ^11.0
  • nunomaduro/collision to ^8.1
  • laravel/breeze to ^2.0 (If installed)
  • laravel/cashier to ^15.0 (If installed)
  • laravel/dusk to ^8.0 (If installed)
  • laravel/jetstream to ^5.0 (If installed)
  • laravel/octane to ^2.3 (If installed)
  • laravel/passport to ^12.0 (If installed)
  • laravel/sanctum to ^4.0 (If installed)
  • laravel/spark-stripe to ^5.0 (If installed)
  • laravel/telescope to ^5.0 (If installed)
  • inertiajs/inertia-laravel to ^1.0 (If installed)

# 移除 doctrine/dbal

doctrine/dbal package 已經被整合進框架原生,所以不用再裝這一個 package,可以從 composer.json 當中移除

# dump migration

11 有做了一項重要修改,之前的 migration 當使用 change 時會自動帶入沒有修改到的 column type,比如原本是 nullable,如果該行只是修改從 integer -> bigInteger 時,不需特別再次指定 nullable。然而 11 開始都必須要特別指定,因此當升級 11 之後若 refresh migration 可能會造成 error

所以解決方法有二

  • 逐一修改所有的 migration files
  • 使用 db:dump,將之前的 migration dump to schema file

第一個解法的工程浩大,我採用第二個

php artisan db:dump

# 升級

準備就緒後,就可以開始升級

composer update

# publish migration

因為 11 之後,migration 不會自動載入,所以如果使用到的 package 有自己的 migration,要匯出

php artisan vendor:publish --tag=cashier-migrations
php artisan vendor:publish --tag=passport-migrations
php artisan vendor:publish --tag=sanctum-migrations
php artisan vendor:publish --tag=spark-migrations
php artisan vendor:publish --tag=telescope-migrations

# 故障排除

執行 composer update 時,如果有套件版本錯誤,就會跳 error。比如套件 A 的版本 1.0 指定 PHP 的版本要 8.1,這時候我們升級將版本改為 8.2 那就無法 update 了

這時候就要去各個套件看一下有沒有支援 Laravel 11,以及有相容的版本分別是多少。查到這些資訊後,到 composer.json 將有問題的 package 版本做修改,然後再次執行 composer update

--

--

Ray Lee | 李宗叡
Learn or Die

It's Ray. I do both backend and frontend, but more focus on backend. I like coding, and would like to see the whole picture of a product.