Laravel 8.65 发布

Laravel

Laravel 团队发布了 8.65:能够在 make 命令期间生成测试文件、String headline 方法、从请求实例中获取数据子集作为集合,以及 v8.x 分支中的最新变化。

String Headline 方法

Steve Bauman 为 StrStringable 贡献了 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 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://laravel-news.com/laravel-8-65-0

译文地址:https://learnku.com/laravel/t/62069

本文为协同翻译文章,如您发现瑕疵请点击「改进」按钮提交优化建议
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!