10月12日,Laravel团队发布了最新的9.35版本,新增了令人兴奋的可替换邮件语法、Eloquent的“严格模式”特性等等。

本周Laravel发布了一些很酷的新内容。首先,你现在可以快速启用Eloquent的“严格模式”:

?‍♂️无惰性加载

❗分配不可填充属性时的异常

?️访问未检索或不存在的属性的异常

Taylor otwell ? (@taylorotwell) 2022年10月11日

可替换的邮件语法

Taylor Otwell贡献了一种新的邮件语法,该语法提供“较少的对象,用于指定可邮件的内容和属性”。

下面是他的pull request描述中的一个例子:

namespace App\Mail;
 
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
 
class InvoicePaid extends Mailable
{
    use Queueable, SerializesModels;
 
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }
 
    /**
     * Get the message envelope.
     *
     * @return \Illuminate\Mail\Mailables\Envelope
     */
    public function envelope()
    {
        return new Envelope(
            subject: 'Invoice Paid',
            cc: [new Address('foo@example.com', 'Example Name')],
            tags: [],
            metadata: [],
        );
    }
 
    /**
     * Get the message content definition.
     *
     * @return \Illuminate\Mail\Mailables\Content
     */
    public function content()
    {
        return new Content(
            view: 'html-view-name',
            text: 'text-view-name',
        );
    }
 
    /**
     * Get the attachments for the message.
     *
     * @return \Illuminate\Mail\Mailables\Attachment[]
     */
    public function attachments()
    {
        return [
            Attachment::fromPath('/path/to/file'),
        ];
    }
}

使用build()定义邮件的传统方式不会被删除。不过我更喜欢上面的例子,因为它很明显地说明了使用PHP 8的命名参数所发生的事情。

Eloquent 严格模式(”strict”)

Chris MorrellTaylor Otwell合作开发了Eloquent严格模式,它实现了以下功能:

  • 没有延迟加载
  • 分配不可填充属性时的异常
  • 访问未检索或不存在的属性的异常

在理想情况下,你应该使用严格模式,方法是在服务提供者的boot()方法中添加以下内容:

Model::shouldBeStrict();

shouldBeStrict()方法是启用以下所有功能的快捷方式:

Model::preventLazyLoading();
Model::preventSilentlyDiscardingAttributes();
Model::preventsAccessingMissingAttributes();

在资源路由中加载软删除模型

Andrew Brown贡献了使用以下路由语法加载软删除模型和资源路由的能力:

// All endpoints
Route::resource('users', UserController::class)->withTrashed();
 
// Only `show`
Route::resource('users', UserController::class)->withTrashed(['show']);

发行说明

你可以在GitHub上看到完整的新特性和更新列表,以及9.34.0和9.35.0之间的差异。以下发布说明直接来自更新日志:

v9.35.0

新增

  • 允许为资源路由加载软删除模型(#44405)
  • 增加了Illuminate/Database/Eloquent/Model::shouldBeStrict()和other ((#44283)
  • 没有解析控制器的控制器中间件(#44516)
  • 可选的邮件语法(#44462)

修复

  • 修复了自引用多对多关系中主列聚合(withSum等)的问题(#44286)
  • 修复了使用静态类属性作为blade属性的问题(#44473)
  • 在EnumerateValues(#44456)中,可遍历应该优先于可序列化JsonSerializable
  • 修正make:cast --inbound,它是一个布尔选项,而不是值(#44505)

更新

  • 测试方法。使用json_encode使错误消息更具可读性(#44397)
  • Model::withoutTimestamps()返回回调的返回值(#44457)
  • 只在相关路由上加载软删除模型(#44478)
  • 为shouldBlockPhpUpload函数添加额外的PHP扩展(#44512)
  • 为噪点特别多的对象注册cutInternals casters(#44514)
  • 使用get方法访问应用程序区域设置(#44521)
  • 只返回来自channels的非空响应(09d53ee3944a3e)
  • 正确的channel匹配(#44531)
  • 迁移邮件组件(#44527)
(adsbygoogle = window.adsbygoogle || []).push({});