Laravel 8.65 发布
Laravel 团队发布了 8.65:能够在 make 命令期间生成测试文件、String headline 方法、从请求实例中获取数据子集作为集合,以及 v8.x 分支中的最新变化。
String Headline 方法
Steve Bauman 为 Str
和 Stringable
贡献了 headline()
方法,将字符串转换为 Studly Words
:
// 显示:「Voice Recording Stored」
echo Str::headline(
class_basename(\App\Events\VoiceRecordingStored::class)
);
// 显示:「Using Laravel With Redis」
echo Str::studlyWords("using-laravel-with-redis");
Gate Policy 回调
Lennart Carstens-Behrens 贡献了将策略回调传递给拦截器的能力:
Gate::inspect([PostPolicy::class, 'update'], $post)->authorize();
Gate::inspect(InvokablePolicy::class)->authorize();
Gate::check([PostPolicy:class, 'update'], $post);
Gate::any([PostPolicy::class, ['update', 'edit']], $post);
如果您想深入了解,请查看 Pull Request #39185.
可配置「记住我」Cookie 持续时间
James Freeman 为「记住我」cookie 贡献了一个可配置的持续时间,通过 config/auth.php
文件即可配置:
// config/auth.php
return [
'remember' => now()->addDays(7)->diffInMinutes(),
];
如果你没有设置对应的配置值,则采取默认配置的过期时间:5年
使用多种 Artisan 命令生成测试
Luke Downing 贡献了使用artisan「make」命令时增加 --test
或者 --pest
后缀去生成测试文件
# 创建单元测试文件
php artisan make:model Product --test
# 创建PEST文件
php artisan make:model Product --pest
如下所列的命令可以使用 --test
或者 --pest
后缀:
make:command
make:job
make:listener
make:mail
make:model
make:notification
make:controller
make:middleware
请求集合的子集合
Iraldo Arévalo Delfín 贡献了一个方法,可以获取到 Request 下 collect()
方法获取到的集合的子集合。
$request = Request::create('/', 'GET', [
'users' => [1, 2, 3],
'roles' => [4, 5, 6],
'email' => 'test@example.com'
]);
// returns [4, 5, 6]
$request->collect(['roles'])->all();
验证多个日期格式
Steve Bauman 贡献了在 date_format
验证规则中多个日期的处理能力。这个验证规则要满足给定的规则其中之一:
public function rules()
{
return [
'date' => 'date_format:Y-m-d,m-d',
];
}
PostgreSQL 的 「update from」 方法
Dries Vints 为 PostgreSQL 语法贡献了一个 updateFrom
方法。
/*
update "users" set "email" = ?, "name" = ? from "orders"
where "users"."id" = ? and "users"."id" = "orders"."user_id"
*/
$builder
->from('users')
->join('orders', 'users.id', '=', 'orders.user_id')
->where('users.id', '=', 1)
->updateFrom(['email' => 'foo', 'name' => 'bar']);
如果要了解详细内容,推荐查看这个 合并请求 #39151
断言重定向包含测试方法
Fredrik 为 TestResponse
实例贡献了 assertRedirectContains()
方法:
$response = TestResponse::fromBaseResponse(
(new Response('', 302))
->withHeaders(['Location' => 'https://url.com'])
);
$response->assertRedirectContains('url.com'); // true
发行说明
你可以在以下 Github 差异对比 8.64.0 and 8.65.0中查看新功能和完整的更新列表。
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。