Laravel 9.46 发布
Laravel团队本周发布了9.46,其中包含两个JsonResource方法,以有条件地包含资源属性、十进制验证规则的更新等:
将 "whenHas" 添加到JSON资源
Michael Nabil 向 JsonResource
提供了一个whenHas()
方法,当在模型上找到属性时,该方法允许你有条件地在Response中包含属性。
例如,这提供了一种有条件地定义属性的干净方法:
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->whenHas('email'),
];
}
也可以将闭包定义为默认值:
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->whenHas('email', function () {
return 'default value';
}),
];
}
参见 Pull Request #45376 了解更多详情。
JsonResource "unless" 方法
Michael Nabil 提供了一个 unless()
方法,用于在条件解析为 false
时检索值:
public function toArray($request)
{
$someCondition = false;
return [
'id' => $this->id,
'name' => $this->name,
// Email will be included, with `true` will not.
'email' => $this->unless($someCondition, 'email'),
];
}
十进制验证规则支持带符号的数字
@Pusparaj 向 decimal
支持签名号码的验证规则 (i.e., -5.5
, +1.5
, etc.):
$v = new Validator(
$trans,
['foo' => '-1.234'],
['foo' => 'Decimal:2,3']
);
$this->assertTrue($v->passes());
Artisan serve 现在传递PATH变量
Maarten Buis 通过php artisan serve
传递 PATH
环境变量 , 这可能是底层代码查找可执行文件所必需的。你可以在 Pull Request #45402 中找到有关的详细信
发行说明
你可以在GitHub上看到下面的新功能和更新的完整列表以及 9.45.0 and 9.46.0 的区别。 以下发行说明直接来自 changelog:
v9.46.0
新增
- 添加了PASTHROWSING PATH变量,用于在 (#45402)
- 已将 whenHas 添加到 JsonResource (#45376)
- 添加 ./fleet 目录到 .gitignore (#45432)
- 除非添加到JsonResource (#45419)
已修复
- 已修复凭据检查 (#45437)
- 已修复十进制转换精度问题 (#45456, #45492)
- 预知验证不会引发验证错误 (#45405)
- 修复了检查自定义强制转换的递增和递减方法的类的问题 (#45444)
已更改
- 更新十进制验证规则以允许验证已签名的数字 (24a48b2)
- 仅输出Vite中的唯一资产/预加载标签 (#45404)
- 优化查询生成器中的 whereKey方法 (#45453)
- 删除 Model.php 中的额外代码以优化性能 (#45476)
- 异常处理程序 prepareResponse 添加上一个异常 (#45499)
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
推荐文章: