Laravel 7.6 发布

Laravel

Laravel团队昨天发布了 v7.6.0,其中包含13个新功能以及 7.x 分支的最新修复和更改:

集合新增 “until” 方法

Jason McCreary 贡献了 Collection::until() 方法, 该方法可以循环遍历集合直到元素满足条件再将该元素返回:

// Before
[$before, $after] = $primes->partition(function ($item) {
    return $item < 11;
});
$before->dump();

// Using until
$passed = $primes->until(11)->dump();

此方法采用闭包或值与集合进行对比。

String Empty Methods

Mark van den Broek 为 Stringable 和 HtmlString 提供了一些便利方法。第一个,HtmlString::isEmpty() 方法让我们检测空实例更加方便:

$string = new \Illuminate\Support\HtmlString(''); 

// Previously
if (empty($string->toHtml()))

// Using isEmpty
if ($string->isEmpty())

其次,Mark 也贡献了 isNotEmpty() 方法

use Illuminate\Support\Stringable;

(new Stringable())->isNotEmpty(); // false
(new Stringable('Hello World'))->isNotEmpty(); // true

Stringable 类的 Trim 方法

Ryan Chandler 为 Stringable 类贡献了 ltrim 和 rtrim 方法,可以修剪字符串开头和结尾的字符:

use Illuminate\Support\Stringable;

echo (new Stringable(' Hello World'))->ltrim(); // 'Hello World'
echo (new Stringable('Hello World '))->rtrim(); // 'Hello World'
echo (new Stringable('/example/'))->rtrim('/'); // '/example'

特定路由忽略中间件

@dsazup 提供了在定义路由时跳过中间件的功能:

Route::get('/something')
    ->skipMiddleware(VerifyCsrfToken::class)
Route::get('/teams/create')
    ->skipMiddleware(VerifyUserHasTeam::class)

Http客户端:获取JSON响应作为对象

Adrian Nürnberger 贡献了 object()方法,可以返回对象形式的 JSON 响应体而不是一个关联数组:

// Array access
Http::get('some-api.wip')['result'];

// Using json()
$response = Http::get('some-api.wip')->json();
$response['result']

// New option
$response = Http::get('some-api.wip')->object();
$response->result;

组件别名

Dries Vints 贡献了 为组件设置别名:

我遇到一个场景,其中我需要根据组件的别名有条件地呈现组件的内容。 例如,当您有一个 Svg 组件并使用 <x:heroicon-o-bell /> 作为该组件的别名时,如下所示:

Blade::component(Svg::class, 'heroicon-o-bell');

这比 <x:svg name="heroicon-o-bell"/> 这种方式更加简洁。 将别名添加到 Component 类将为 Blade组件增加许多新的用法和可能性...

Append Attributes Across an Eloquent Collection

Niels Faurskov 贡献了一个 eloquent 集合方法 append() ,他可以向集合中附加特定属性:

// Before Laravel 7.6
$collection->each(function($model) {
    $model->append($attribute)
});

// Append method
$collection->append($attribute);

支持 Retry-After 方法

@RyanDaDeng 贡献了个方法级的支持,他可以对队列监听器的  retryAfter 进行补充,以适用更高级的用例:

// listener implementation

public function retryAfter()
{
    // 自定义 retryAfter 逻辑
}

支持 Composer 新版 installed.json 格式

Jakub Arbet 支持 Composer 2 新版本的快照功能(尚未稳定), 但仍与旧版本的composer向后兼容:

在 composer 的最新快照版本中更改了 vendor/composer/installed.json 的格式,从而破坏了自动发现软件包的功能。 此 PR 通过较早版本的 composer 向后兼容来解决此问题。

UUID 支持更改

Mathieu Tudisco 支持在 uuid 列使用 change()  方法, 在此之前会导致以下错误:

Unknown column type “uuid” requested.

发行说明

您可以在下面查看 GitHub 上的新功能和更新的完整列表以及7.5.0 and 7.6.0](https://github.com/laravel/framework/compa...) 之间的区别。 Laravel 7.x 的完整发行说明可在最新的v7 changelog中找到:

v7.6.0

新增

  • 新增 Collection::until() 方法 (#32262)
  • 新增 HtmlString::isEmpty() 方法 (#32289#32300)
  • 新增 Illuminate\Support\Stringable::isNotEmpty() 方法 (#32293)
  • Illuminate\Support\Stringable 类新增 ltrim() 和 rtrim() 方法 (#32288)
  • 新增忽略中间件的功能 (#32347412261c)
  • 新增 Illuminate\Http\Client\Response::object() 方法 (#32341)
  • 支持设置组件别名 (#32346)
  • 新增 Illuminate\Database\Eloquent\Collection::append() 方法 (#32324)
  • BelongsToMany 的 pivot 列新增 “between” 语句 (#32364)
  • 队列监听支持 retryAfter() 方法 (#32370)
  • 新增对 composer 新版 installed.json 的格式支持 (#32310)
  • 数据库迁移文件新增 uuid 更改支持(#32316)
  • 允许保存资源到 postgresql bytea (#32319)

修复

  • 修复 phpredis 的 *scan 方法 (#32336)
  • 修复 Illuminate\Auth\Notifications\ResetPassword::toMail() (#32345)
  • Illuminate\Translation\Translator::__construct() 调用 setLocale  (1c6a504)
  • 使用映射来防止不必要的数组访问 in Illuminate\Http\Resources\Json\PaginatedResourceResponse::toResponse() (#32296)
  • 当 pivot 未被修改的时候阻止时间戳更新 (#32311)
  • 修复 CURRENT_TIMESTAMP 在 Illuminate\Database\Schema\Grammars\MySqlGrammar  中的精度 bug (#32298)

修改

  • HtmlString 的构造函数增加默认值 (#32290)
  • 使用 BindingResolutionException 标示容器解析问题 (#32349)
  • Illuminate\Validation\Concerns\ValidatesAttributes.php ::validateUrl() 使用 Symfony/Validator 5.0.7 匹配 (#32315)

弃用

  • 弃用 elixir 函数 (#32366)
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://laravel-news.com/laravel-7-6-0

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

本文为协同翻译文章,如您发现瑕疵请点击「改进」按钮提交优化建议
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 6

感觉 laravel 坐上了火箭 :joy: :joy: :joy:

4年前 评论

@一个人的江湖 :joy: 还真是,顺便感谢译者们!

4年前 评论

这个功能我觉得不是很 elegant :see_no_evil: file

4年前 评论

skipMiddleware 应该是 withoutMiddleware 亲测

可看 git log 和代码,已经从 skip 改为 without

4年前 评论

@RyanDaDeng 就是我哈哈哈。。。

4年前 评论

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