Laravel 6.7.0 版本发布


Laravel 开发团队本周发布了 v6.7.0 版本,新增了一些新特性,以及对之前版本问题的修复。

新增特性

下面我们对其中一些比较值得关注的代码调整做简单的介绍:

Eloquent 模型类中引入的 HasTimestamps Trait 新增了两个方法用来返回完整的创建和更新时间字段名:

$model->getQualifiedCreatedAtColumn();
// 例如 users.created_at

$model->getQualifiedUpdatedAtColumn();
// 例如 users.updated_at

异常处理器中可以使用新的 exceptionContext() 方法来提供额外的自定义日志上下文:

// App/Exceptions/Handler.php
protected function exceptionContext(Exception $e)
{
    if ($e instanceof CustomException) {
        return ['custom_context' => $e->getCustomProperty()];
    }

    return parent::exceptionContext($e);
}

邮件传输失败时,现在会抛出错误来取代之前的静默失败,此更新无需调整任何上层业务代码:

TransportManager底层代码调整

此外,一个比较重要的更新是 Eloquent 模型类现在还新增了 withoutRelations() 方法,用来支持在队列任务中不加载关联关系,从而提高性能。我们可以在队列任务的构造函数中调用该方法:

/**
 * Create a new job instance.
 *
 * @param  \App\Podcast  $podcast
 * @return void
 */
public function __construct(Podcast $podcast)
{
    $this->podcast = $podcast->withoutRelations();
}

具体细节可以参考 Laravel 6 队列文档

资源集合现在可以通过调用 preserveQueryParameters() 方法在 API 资源分页时保留查询字符串:

return MyResourceCollection::make($repository->paginate())->preserveQueryParameters();

更新日志

新增方法

  • HasTimestamps 中新增 getQualifiedCreatedAtColumn()getQualifiedUpdatedAtColumn() 方法(#30792
  • Exceptions\Handler 中新增 exceptionContext() 方法支持(#30780
  • 邮件传输底层出错时抛出错误(#30799, 4320b82
  • HasRelationships 中新增 withoutRelations()unsetRelations() 方法(#30802
  • 新增 ResourceCollection::preserveQueryParameters() 方法以便在 API 资源分页时保留查询字符串(#30745, e92a708

修复问题

  • 修复基于字符串的数据库验证规则中的显式模型问题(#30790
  • 修复 Routing\RedirectController() 问题(#30783

代码调整

  • PhpRedisConnection 重连机制调整(#30778
  • 优化 ShouldBroadcastNow 性能(#30797, 5b3cc97

声明:以上内容整理翻译自 Laravel News


点赞 取消点赞 收藏 取消收藏

<< 上一篇: Leetcode PHP题解--D121 21. Merge Two Sorted Lists

>> 下一篇: Laravel 6.8.0 版本发布