Laravel 9.49 发布
Laravel团队本周发布了 9.49,支持枚举数组的造型、CLI 提示等。请查看变更日志,因为本周的发布中充满了过去两周的新添加、修复和更改;上周,Laravel 团队的大部分成员都在 Laracon EU。
支持枚举集合或数组的造型
Ralph J. Smit 提供了枚举数组的造型:
use App\Enums\ServerStatus;
use Illuminate\Database\Eloquent\Casts\AsEnumCollection;
protected $casts = [
'statuses' => AsEnumCollection::class.':'.ServerStatus::class,
];
下例是数组版本的例子:
use App\Enums\ServerStatus;
use Illuminate\Database\Eloquent\Casts\AsEnumArrayObject;
protected $casts = [
'statuses' => AsEnumArrayObject::class.':'.ServerStatus::class,
];
CLI 提示
Jess Archer 提供了命令行中参数缺失自动提示用户的功能,替换原来返回错误的方式。该特性可以通过执行 PromptsForMissingInput
接口来实现:
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;
class MyCommand extends Command implements PromptsForMissingInput
{
// ...
}
该特性也被添加到所有的 make:* 命令中。
新增 TestResponse JSON 断言
Seth Phat 为断言 JSON 为数组及 JSON 为对象提供了两个断言方法:
$this->json('GET', 'countries')
->assertOk()
->assertJsonIsArray(); // [ {..}, {...}, ....]
$this->json('GET', 'users')
->assertOk()
->assertJsonIsArray('data'); // {'data': [...]}
$this->json('GET', 'countries/US')
->assertOk()
->assertJsonIsObject(); // {id: '...', name: '..'}
$this->json('GET', 'users/1')
->assertOk()
->assertJsonIsObject('data'); // {'data': {id: '..', name: '...'}}
"missing" 验证规则
Tim MacDonald 提供了 "missing" 验证规则,它是 prohibits 验证规则的严格版。 该验证规则下的字段不能在输入数据中存在。
以下是所有可能的变化:
- missing
- missing_if:attribute,value
- missing_unless:attribute,value
- missing_with:attribute1,attribute2
- missing_with_all:attribute1,attribute2
HTTP client error handling methods
Wendell Adriel 为错误处理提供了新的 HTTP 响应方法:
Today on #Laravel 9.49 release:
The new methods for the HTTP Client from my PR are available 🤩🔥🚀💪
$response->clientError();
$response->serverError();
$response->throwIfStatus(403);
$response->throwUnlessStatus(200);
You can check the docs here:https://t.co/ETLJRTUKNv— Wendell Adriel (@wendell_adriel) January 31, 2023
示例:
// Only throws an exception if the HTTP response code is 500
$response->throwIfStatus(500);
// Callable
$response->throwIfStatus(fn ($status) => $status === 500);
// Throws an exception if the HTTP response code is not 200
$response->throwUnlessStatus(200);
// Only throws an exception if the HTTP response code is >= 400 and < 500
$response->throwIfClientError();
// Only throws an exception if the HTTP response code is >= 500
$response->throwIfServerError();
为队列worker输出配置时区支持
Matias Mäki 提供了一个 queue.log_timezone
配置项,以便 queue:work
可以在不同时区下输出时间戳,以区别于应用默认的 app.timezone
设置:
The rationale for this addition is that this is already doable for log files made through Log facade with Log::setTimezone, but the WorkCommand is not using the Logging subsystem but Console\OutputStyle to write directly back to the stdout and stderr.
删除外键无操作
Erfan Hemmati 为外键迁移提供了 noActionOnDelete()
方法
$table
->foreign('user_id')
->references('id')
->on('users')
->noActionOnDelete();
在软删除模型中添加静默强制删除
Pascal Huberts 提供了一个 forceDeleteQuietly()
方法,该方法在不引发任何事件的情况下强制对软删除模型进行硬删除:
$model->forceDeleteQuietly();
数组 sortDesc() 方法
Timur Fralik 提供了一个 Arr::sortDesc()
方法,用来以降序方式对数组的值进行排序:
$sorted = Arr::sortDesc(['Desk', 'Table', 'Chair']);
// ['Table', 'Desk', 'Chair']
Release Notes
You can see the complete list of new features and updates below and the diff between 9.48.0 and 9.49.0 on GitHub. The following release notes are directly from the changelog:
v9.49.0
Added
- Added Illuminate/Database/Schema/ForeignKeyDefinition::noActionOnDelete() (#45712)
- Added new throw helper methods to the HTTP Client (#45704)
- Added configurable timezone support for WorkCommand output timestamps (#45722)
- Added support for casting arrays containing enums (#45621)
- Added "missing" validation rules (#45717)
- Added /Illuminate/Database/Eloquent/SoftDeletes::forceDeleteQuietly() (#45737)
- Added Illuminate/Collections/Arr::sortDesc() (#45761)
- Added CLI Prompts (#45629, #45864)
- Adds assertJsonIsArray and assertJsonIsObject for TestResponse (#45731)
- Added Illuminate/Database/Eloquent/Relations/HasOneOrMany::createQuietly() (#45783)
- Add validation rules: ascii_alpha, ascii_alpha_num, ascii_alpha_dash (#45769)
- Extract status methods to traits (#45789)
- Add "addRestoreOrCreate" extension to SoftDeletingScope (#45754)
- Added connection established event (f850d99)
- Add forceDeleting event to models (#45836)
- Add title tag in mail template (#45859)
- Added new methods to Collection (#45839)
- Add skip cancelled middleware (#45869)
Fixed
- Fix flushdb on cluster for PredisClusterConnection.php (#45544)
- Fix blade tag issue with nested calls (#45764)
- Fix infinite loop in blade compiler (#45780)
- Fix ValidationValidator not to accept terminating newline (#45790)
- Fix stubs publish command generating incorrect controller stubs (#45812)
- fix: normalize route pipeline exception (#45817)
- Fix Illuminate Filesystem replace() leaves file executable (#45856)
Changed
- Ensures channel name matches from start of string (#45692)
- Replace raw invisible characters in regex expressions with counterpart Unicode regex notations (#45680)
- Optimize destroy method (#45709)
- Unify prohibits behavior around prohibits_if (#45723)
- Removes dependency on bcmath (#45729)
- Allow brick/math 0.11 also (#45762)
- Optimize findMany of BelongsToMany (#45745)
- Ensure decimal rule handles large values (#45693)
- Backed enum support for @js (#45862)
- Restart syscalls for SIGALRM when worker times out a job (#45871)
- Ensure subsiquent calls to Mailable->to() overwrite previous entries (#45885)