laravel-upgrade

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Laravel Upgrade

Laravel 版本升级

Upgrade Laravel applications one major version at a time. Supports: 9→10, 10→11, 11→12.
一次将Laravel应用升级一个主版本。支持:9→10、10→11、11→12。

Workflow

工作流程

1. Detect Current Version

1. 检测当前版本

Read
composer.json
and find the
laravel/framework
version constraint:
php
// Example: "laravel/framework": "^10.0" means Laravel 10.x
Determine target version (current + 1). If already on Laravel 12, inform user they're on the latest supported version.
读取
composer.json
并查找
laravel/framework
的版本约束:
php
// 示例:"laravel/framework": "^10.0" 表示 Laravel 10.x
确定目标版本(当前版本+1)。如果当前已是Laravel 12,则告知用户当前使用的是支持的最新版本。

2. Load Upgrade Guide

2. 加载升级指南

Based on detected versions, read the appropriate reference file:
CurrentTargetReference File
9.x10.xreferences/from-9-to-10.md
10.x11.xreferences/from-10-to-11.md
11.x12.xreferences/from-11-to-12.md
根据检测到的版本,读取对应的参考文件:
当前版本目标版本参考文件
9.x10.xreferences/from-9-to-10.md
10.x11.xreferences/from-10-to-11.md
11.x12.xreferences/from-11-to-12.md

3. Scan and Fix

3. 扫描与修复

For each breaking change in the guide, scan the codebase and apply fixes:
High Impact (always check):
  • composer.json
    dependency versions
  • PHP version requirements
  • Database migrations using deprecated methods
Medium Impact (check relevant files):
  • Model
    $dates
    property →
    $casts
    (9→10)
  • Database expressions with
    (string)
    casting (9→10)
  • Column modification migrations missing attributes (10→11)
  • HasUuids
    trait behavior change (11→12)
Low Impact (check if patterns found):
  • Deprecated method calls (
    Bus::dispatchNow
    ,
    Redirect::home
    , etc.)
  • Contract interface changes
  • Configuration file updates
针对指南中的每一项破坏性变更,扫描代码库并应用修复:
高影响(必须检查)
  • composer.json
    依赖版本
  • PHP版本要求
  • 使用已弃用方法的数据库迁移
中影响(检查相关文件)
  • 模型
    $dates
    属性 →
    $casts
    (9→10)
  • 带有
    (string)
    强制转换的数据库表达式(9→10)
  • 缺少属性的列修改迁移(10→11)
  • HasUuids
    trait行为变更(11→12)
低影响(如果发现对应模式则检查)
  • 已弃用方法调用(
    Bus::dispatchNow
    Redirect::home
    等)
  • 契约接口变更
  • 配置文件更新

4. Update Dependencies

4. 更新依赖

After code fixes, update
composer.json
:
bash
undefined
完成代码修复后,更新
composer.json
bash
undefined

Update laravel/framework constraint to target version

将laravel/framework约束更新为目标版本

Update related packages per upgrade guide

根据升级指南更新相关包

composer update
undefined
composer update
undefined

5. Post-Upgrade Verification

5. 升级后验证

  • Run
    php artisan
    to verify framework boots
  • Run test suite if available
  • Check for deprecation warnings in logs
  • 运行
    php artisan
    验证框架能否正常启动
  • 如果有测试套件则运行测试
  • 检查日志中的弃用警告

Common Patterns

常见模式

Dependency Updates (all upgrades)

依赖更新(所有升级通用)

Search
composer.json
for outdated constraints and update per guide.
composer.json
中搜索过时的约束并根据指南进行更新。

Model $dates to $casts (9→10)

模型$dates转为$casts(9→10)

php
// Before
protected $dates = ['deployed_at'];

// After
protected $casts = ['deployed_at' => 'datetime'];
Search pattern:
protected \$dates\s*=
php
// 升级前
protected $dates = ['deployed_at'];

// 升级后
protected $casts = ['deployed_at' => 'datetime'];
搜索模式:
protected \$dates\s*=

Database Expression Casting (9→10)

数据库表达式强制转换(9→10)

php
// Before
$string = (string) DB::raw('select 1');

// After
$string = DB::raw('select 1')->getValue(DB::connection()->getQueryGrammar());
php
// 升级前
$string = (string) DB::raw('select 1');

// 升级后
$string = DB::raw('select 1')->getValue(DB::connection()->getQueryGrammar());

Column Modification (10→11)

列修改(10→11)

Migrations using
->change()
must now include all modifiers:
php
// Before (implicit retention)
$table->integer('votes')->nullable()->change();

// After (explicit)
$table->integer('votes')->unsigned()->default(1)->nullable()->change();
使用
->change()
的迁移现在必须包含所有修饰符:
php
// 升级前(隐式保留)
$table->integer('votes')->nullable()->change();

// 升级后(显式声明)
$table->integer('votes')->unsigned()->default(1)->nullable()->change();

HasUuids Trait (11→12)

HasUuids Trait(11→12)

php
// Before (ordered UUIDv4)
use Illuminate\Database\Eloquent\Concerns\HasUuids;

// After (if you need UUIDv4 behavior)
use Illuminate\Database\Eloquent\Concerns\HasVersion4Uuids as HasUuids;
php
// 升级前(有序UUIDv4)
use Illuminate\Database\Eloquent\Concerns\HasUuids;

// 升级后(如果需要UUIDv4行为)
use Illuminate\Database\Eloquent\Concerns\HasVersion4Uuids as HasUuids;