Laravel 10.4版本发布

2023-06-01 00:00:00 laravel 版本 发布

Laravel团队发布了10.4版本,增加了File::json()方法,将现有的HasMany关系转换为HasOne关系,新的测试响应断言,以及更多。


File::json()方法

Austin White贡献了一个File::json()方法, 以方便从文件中获取JSON编码的数据:

// Before
$contents = File::get('sample.json');
$data = json_decode($contents, true);
 
// After
$data = File::json('sample.json');


断言不支持的媒体类型

Shamimul Alam为415不支持的媒体类型响应状态代码贡献了一个断言帮助器:

$response->assertUnsupportedMediaType();


将现有的HasMany转换为HasOne关系

Luke Kuzmish贡献了将HasMany转换为HasOne和将MorphMany转换为MorphOne的内容。

以这个必须定义两个关系的例子为例:

class User extends Model
{
    public function logins(): HasMany {
        return $this->hasMany(Login::class, 'some_id', 'the_other_id');
    }
    public function latestLogin(): HasOne {
        return $this->hasOne(Login::class, 'some_id', 'the_other_id')->latestOfMany();
    }
}

现在有了这个PR,你可以用->one()方法来代替做以下事情:

class User extends Model
{
    public function logins(): HasMany {
        return $this->hasMany(Login::class, 'some_id', 'the_other_id');
    }
    public function latestLogin(): HasOne {
        return $this->logins()->one()->latestOfMany();
    }
}

one()方法对HasMany、HasManyThrough和MorphMany可用。


为paginationInformation创建可宏方法

Frans Slabbekoorn 贡献了为 paginationInformation 定义一个宏的能力

它允许自定义分页信息,而不必将所有资源作为基础资源来扩展:

/** @mixin \Illuminate\Http\Resources\Json\ResourceCollection */
class ResourceCollectionMixin
{
    public function paginationInformation(): Closure
    {
        return fn ($request, $paginated, $default) => collect($default)->mapWithKeysRecursively(fn ($item, $key) => [Str::camel($key) => $item])->toArray();
    }
}


发布说明

你可以在GitHub上看到以下完整的新功能和更新列表以及10.3.0和10.4.0之间的差异.

下面的发布说明直接来自更新日志:

https://github.com/laravel/framework/compare/v10.3.0...v10.4.0
https://github.com/laravel/framework/blob/7046e9bd5f5957f12865a8326385aaa2a57ff067/CHANGELOG.md#v1040-2023-03-17


v10.4.0

已添加

已添加 Illuminate/Testing/Concerns/AssertsStatusCodes::assertUnsupportedMediaType() (#46426)
添加了 curl_error_code。77 到 DetectsLostConnections (#46429)
允许将HasMany转换为HasOne &&MorphMany转换为MorphOne (#46443)
为分页信息(paginationInformation)增加创建可宏方法的选项(#46461)。
增加了Illuminate/Filesystem/Filesystem::json() (#46481)

修复

修复了使用调度器重新路由的命令事件的解析输入参数 (#46442)
修复枚举的使用与可选隐式参数(#46483)。
修复了symfony mailer中嵌入图片的弃用问题(#46488)

更改

在Postgres DSN中增加了备选数据库端口 (#46403)
允许在基于封闭的路由上调用getControllerClass (#46411)
删除过时的method_exists(ReflectionClass::class, 'isEnum')调用 (#46445)
将whereExists中的口才构建器转换为基础构建器(#46460)
重构共享静态methodExcludedByOptions方法为trait (#46498)


转:

https://laravel-news.com/laravel-10-4-0

相关文章