升级指南
这是一篇协同翻译的文章,你可以点击『我来翻译』按钮来参与翻译。
升级指南
高影响变更
中等影响变更
低影响变更
从 11.x 升级到 12.0
预计升级时间:5 分钟
[!NOTE]
我们尝试记录每一个可能的重大变更。由于这些变更中的一些位于框架的偏僻部分,只有一部分变更可能实际影响您的应用程序。想节省时间吗?您可以使用 Laravel Shift 来帮助自动化您的应用程序升级。
更新依赖项
影响可能性:高
您应该在应用程序的 composer.json
文件中更新以下依赖项:
laravel/framework
到^12.0
phpunit/phpunit
到^11.0
pestphp/pest
到^3.0
Carbon 3
影响可能性:低
对 Carbon 2.x 的支持已被移除。所有 Laravel 12 应用程序现在需要 Carbon 3.x.
更新 Laravel 安装程序
如果您使用 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's 捆绑的 Laravel 安装程序副本,您应该将您的 Herd 安装更新到最新版本。
认证
更新后的 DatabaseTokenRepository 构造函数签名
影响可能性:极低
Illuminate\Auth\Passwords\DatabaseTokenRepository
类的构造函数现在要求 $expires
参数以秒为单位传递,而不是以分钟为单位。
并发
并发结果索引映射
影响可能性:低
当使用关联数组调用 Concurrency::run
方法时,并发操作的结果现在会返回其关联的键:
$result = Concurrency::run([
'task-1' => fn () => 1 + 1,
'task-2' => fn () => 2 + 2,
]);
// ['task-1' => 2, 'task-2' => 4]
容器
容器类依赖解析
影响可能性:低
依赖注入容器现在在解析类实例时尊重类属性的默认值。如果您之前依赖容器在没有默认值的情况下解析类实例,您可能需要调整您的应用程序以适应这种新行为:
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
Multi-Schema Database Inspecting
Likelihood Of Impact: Low
The Schema::getTables()
, Schema::getViews()
, and Schema::getTypes()
methods now include the results from all schemas by default. You may pass the schema
argument to retrieve the result for the given schema only:
// All tables on all schemas...
$tables = Schema::getTables();
// All tables on the 'main' schema...
$tables = Schema::getTables(schema: 'main');
// All tables on the 'main' and 'blog' schemas...
$tables = Schema::getTables(schema: ['main', 'blog']);
The Schema::getTableListing()
method now returns schema-qualified table names by default. You may pass the schemaQualified
argument to change the behavior as desired:
$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']
The db:table
and db:show
commands now output the results of all schemas on MySQL, MariaDB, and SQLite, just like PostgreSQL and SQL Server.
Updated Blueprint
Constructor Signature
Likelihood Of Impact: Very Low
The constructor of the Illuminate\Database\Schema\Blueprint
class now expects an instance of Illuminate\Database\Connection
as its first argument.
Eloquent
Models and UUIDv7
Likelihood Of Impact: Medium
The HasUuids
trait now returns UUIDs that are compatible with version 7 of the UUID spec (ordered UUIDs). If you would like to continue using ordered UUIDv4 strings for your model's IDs, you should now use the HasVersion4Uuids
trait:
use Illuminate\Database\Eloquent\Concerns\HasUuids; // [tl! remove]
use Illuminate\Database\Eloquent\Concerns\HasVersion4Uuids as HasUuids; // [tl! add]
The HasVersion7Uuids
trait has been removed. If you were previously using this trait, you should use the HasUuids
trait instead, which now provides the same behavior.
Requests
Nested Array Request Merging
Likelihood Of Impact: Low
The $request->mergeIfMissing()
method now allows merging nested array data using "dot" notation. If you were previously relying on this method to create a top-level array key containing the "dot" notation version of the key, you may need to adjust your application to account for this new behavior:
$request->mergeIfMissing([
'user.last_name' => 'Otwell',
]);
Validation
Image Validation Now Excludes SVGs
The image
validation rule no longer allows SVG images by default. If you would like to allow SVGs when using the image
rule, you must explicitly allow them:
use Illuminate\Validation\Rules\File;
'photo' => 'required|image:allow_svg'
// Or...
'photo' => ['required', File::image(allowSvg: true)],
Miscellaneous
We also encourage you to view the changes in the laravel/laravel
GitHub repository. While many of these changes are not required, you may wish to keep these files in sync with your application. Some of these changes will be covered in this upgrade guide, but others, such as changes to configuration files or comments, will not be. You can easily view the changes with the GitHub comparison tool and choose which updates are important to you.
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
推荐文章: