编程

Laravel 9.2.0 发布了!新特性: 数组的keyBy方法,Eloquent 属性的静态构造器,将Laravel CORS 扩展包添加到框架中,等

1294 2022-02-25 16:47:48

Laravel 发布了 9.2.0 版本,新特性包括: 数组的 keyBy 方法,Eloquent 属性的静态构造器,将 Laravel CORS 扩展包添加到框架中,等等:

Attribute make 方法

@ARI 为Eloquent Attribute 类贡献了一个静态构造方法, 提供了如下便利构造方式:

// Using the new keyword
return (new Attribute(
    get: fn ($value) => strtoupper($value),
    set: fn ($value) => strtoupper($value)
))->withoutObjectCaching();
 
// The new make() static constructor method
return Attribute::make(
    get: fn ($value) => strtoupper($value),
    set: fn ($value) => strtoupper($value)
)->withoutObjectCaching();

数组keyBy方法

Douglas Medeiros 贡献了 Arr::keyBy() 方法,像集合的 keyBy() 方法一样: 

$array = [
    ['id' => '123', 'data' => 'abc', 'device' => 'laptop'],
    ['id' => '345', 'data' => 'def', 'device' => 'tablet'],
    ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'],
];
 
Arr::keyBy($array, 'id');
/*
[
    '123' => ['id' => '123', 'data' => 'abc', 'device' => 'laptop'],
    '345' => ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone']
    // The second element of an original array is overwritten by the last element because of the same id
]
*/

输出包含的测试断言

Francisco Madeira 贡献了 expectsOutputToContain 测试方法,用以断言 artisan 命令是否包含输入的子串:

$this->artisan('Hello World')
     ->expectsOutputToContain('Hello');

使用 Mail::alwaysTo 时添加 X header

Craig Morris 贡献了在开发中添加 X header的方法,用于使用 Mail::alwaysTo()方法时 :

When using Mail::alwaysTo in development environments, the original To, Cc and Bcc are lost. This makes it difficult to determine where the email was going to during testing.

This PR adds the original to, cc and bcc into X-Headers in the email so this information can be retrieved, while still preventing the email being sent to these recipients.

这对于调试to, cc和bcc字段有用,仍然只发送邮件到指定的alwaysTo地址。

将 Laravel CORS 包集成到框架中

Dries Vintsfruitcake/laravel-cors 包集成到了 Laravel 框架中:

The main reason is that we want to remove a circular dependency we rely on additionally to the fact that we eliminate another dependency of the skeleton.

All credits for the code go to @barryvdh of @fruitcake . Thanks for maintaining that package for so long!

字符串 "Between First" 方法

Yoeri Boven 贡献了 betweenFirst() 方法,用于获取字符串中给定的两个值之间的最小可能部分:

Str::betweenFirst('[a]ab[b]', '[', ']'); // a
Str::betweenFirst('foofoobar', 'foo', 'bar'); // foo
Str::betweenFirst('hannah', 'ha', 'ah'); // nn
Str::betweenFirst('dddabcddd', 'a', 'c')); // b

允许为Rule对象指定自定义消息

Ryan Chandler 贡献了用 Rule 对象验证时可以指定自定义错误消息的方法。伴随此次更新,你可以为 messages 数组提供自定义消息:

$request->validate(
    [
        'foo' => [new Example]
    ],
    [
        Example::class => 'My custom message goes here!'
    ]
);

v9.2.0

Added

  • Added Illuminate/Database/Eloquent/Casts/Attribute::make() (#41014)
  • Added Illuminate/Collections/Arr::keyBy() (#41029)
  • Added expectsOutputToContain to the PendingCommand. (#40984)
  • Added ability to supply HTTP client methods with JsonSerializable instances (#41055)
  • Added Illuminate/Filesystem/AwsS3V3Adapter::getClinet() (#41079)
  • Added Support for enum in Builder::whereRelation (#41091)
  • Added X headers when using Mail::alwaysTo (#41101)
  • Added of support Bitwise operators in query (#41112)
  • Integrate Laravel CORS into framework (#41137)
  • Added Illuminate/Support/Str::betweenFirst() (#41144)
  • Allow specifiying custom messages for Rule objects (#41145)

Fixed

  • Fixed Queue Failed_jobs insert issue with Exception contain UNICODE (#41020)
  • Fixes attempt to log deprecations on mocks (#41057)
  • Fixed loadAggregate not correctly applying casts (#41050)
  • Do not transform JsonSerializable instances to array in HTTP client methods (#41077)
  • Fix parsing config('database.connections.pgsql.search_path') (#41088)
  • Eloquent: firstWhere returns Object instead of NULL (#41099)
  • Fixed updated with provided qualified updated_at (#41133)
  • Fix setPriority Call for MailChannel (#41120)
  • Fixed route:list command output (#41177)
  • Fix database migrations $connection property (#41161)

Changed

  • Cursor pagination: convert original column to expression (#41003)
  • Cast $perPage to integer on Paginator (#41073)
  • Restore S3 client extra options (#41097)
  • Use latest() within notifications() in Illuminate/Notifications/HasDatabaseNotifications.php (#41095)
  • Remove duplicate queries to find batch (#41121)
  • Remove redundant check in FormRequest::validated() (#41115)
  • Illuminate/Support/Facades/Storage::fake() changed (#41113)
  • Use coalesce equal as provided by PHP >= 7.4 (#41174)
  • Simplify some conditions with is_countable() (#41168)
  • Pass AWS temporary URL options to createPresignedRequest method (#41156)
  • Let Multiple* exceptions hold the number of records and items found (#41164)
  •