Laravel 10.43版本发布中添加流JSON响应
Laravel  /  管理员 发布于 1年前   314
Laravel 团队发布了 v10.43,新增了对流 JSON 响应的支持、通过类名注册全局 Eloquent 作用域、
新的 insertOrIgnoreUsing Eloquent 方法等。
该版本于 1 月 30 日发布,但我们希望重点介绍其中新增的一些出色功能。
下面是 Laravel 10 最新发布的新特性:
增加对流 JSON 响应的支持
Peter Elmered 为 Symphony 6.3 中添加的 StreamedJsonResponse 提供了支持。
https://github.com/pelmered
这样做的好处是减少了大数据有效载荷的内存占用。
下面是拉取请求描述中的一个示例,说明如何使用它:
https://github.com/laravel/framework/pull/49873#issue-2103868933
namespace App\Http\Controllers;
use Generator;
use App\Models\User;
class ExampleController extends Controller
{
public function index()
{
return response()->streamJson([
'users' => $this->yieldUsers(),
]);
}
protected function yieldUsers(): Generator
{
foreach (User::query()->cursor() as $user) {
yield $user;
}
}
}
与 streamJson 方法一起推出的还有一个新的测试助手,用于断言流式 JSON 内容:
$response->assertStreamedJsonContent([
'data' => [
['id' => 1],
['id' => 2],
['id' => 3],
],
]);
您可以从 HttpFoundation Component 的 JSON 响应流文档中了解有关
Symphony 这项功能的更多信息。
https://symfony.com/doc/6.3/components/http_foundation.html#streaming-a-json-response
新的 Eloquent insertOrIgnoreUsing 方法
Trevor Morris 提供了一种 insertOrIgnoreUsing 方法,该方法 "允许在支持'忽略'功能的连接器中,
在出现键冲突时使用子选择成功进行批量插入":
$result = $builder->from('table1')->insertOrIgnoreUsing(
['foo'],
function (Builder $query) {
$query->select(['bar'])->from('table2')->where('foreign_id', '=', 5);
}
);
// MySQL:
// insert ignore into `table1` (`foo`) select `bar` from `table2` where `foreign_id` = ?'
// PostgreSQL:
// insert into "table1" ("foo") select "bar" from "table2" where "foreign_id" = ? on conflict do nothing
新的 Schema hasIndex() 方法
Hafez Divandari 提供了 Schema::hasIndex() 方法和其他一些方法,使用方法如下:
Schema::hasIndex('users', 'my_index_name');
Schema::hasIndex('users', ['email']);
Schema::hasIndex('users', ['email'], 'unique');
Schema::hasIndex('users', ['id'], 'primary');
Schema::hasIndex('users', ['name', 'title'], 'fulltext');
Schema::getTableListing(); // string[]
Schema::getIndexListing('users'); // string[]
添加全局作用域时使用类名
Eliezer Margareten 提供了在向模型添加全局作用域时传递作用域类名的功能:
/**
* The "booted" method of the model.
*/
protected static function booted(): void
{
static::addGlobalScope(AncientScope::class);
}
除此更新外,埃利泽还贡献了 addGlobalScopes() 方法,
它可以让你一次注册多个全局作用域:
/**
* The "booted" method of the model.
*/
protected static function booted(): void
{
static::addGlobalScopes([FirstScope::class, SecondScope::class]);
}
新的存储:unlink Artisan 命令
Mikhail Salkov 提供了一条新的 Artisan 命令,可删除所有符号链接:
php artisan storage:unlink
发布说明
您可以在 GitHub 上查看以下新功能和更新的完整列表,以及 10.42.0 和 10.43.0 之间的差异。
以下版本说明直接来自更新日志:
https://github.com/laravel/framework/compare/v10.42.0...v10.43.0
https://github.com/laravel/framework/blob/1d0f002172d1570389e72fb474d16a6996fc943d/CHANGELOG.md#v10430---2024-01-30
v10.43.0
[10.x] 添加 storage:unlink 命令
(由 @salkovmx 在 https://github.com/laravel/framework/pull/49795 提供
[10.x] 将 \Illuminate\Log\LogManager 方法定义注释与 \Psr\Logger\Interface 统一,
作者 @eusonlito,发布于 https://github.com/laravel/framework/pull/49805
[10.x] 全局作用域的类名字符串参数
(由 @emargareten 在 https://github.com/laravel/framework/pull/49802 提供
[10.x] 添加 hasIndex() 和少量模式增强
@hafezdivandari 发表在 https://github.com/laravel/framework/pull/49796
[10.x] 使用 withoutTouching 时不要触及 BelongsToMany 关系
(由 @mateusjunges 发布于 https://github.com/laravel/framework/pull/49798
[10.x] 在与视图共享之前检查邮件的属性是否初始化
(由 @j3j5 在 https://github.com/laravel/framework/pull/49813 提供
[10.x] 删除队列工作流中的重复操作/结账
(由 @Jubeki 在 https://github.com/laravel/framework/pull/49828 提供
[10.x] 为 Eloquent 添加 insertOrIgnoreUsing
by @trovster in https://github.com/laravel/framework/pull/49827
[10.x] 让 hasIndex() 对顺序敏感
by @hafezdivandari in https://github.com/laravel/framework/pull/49840
[10.x] 由 @driesvints 在 https://github.com/laravel/framework/pull/49838
中提出的发布行动
[10.x] 添加 MariaDb1060Platform
by @driesvints in https://github.com/laravel/framework/pull/49848
[10.x] 统一透视和模型文档块 $guarded
by @eusonlito in https://github.com/laravel/framework/pull/49851
[10.x]引入 beforeStartingTransaction 回调并在 LazilyRefreshDatabase 中使用,
作者 @pascalbaljet,原文地址:https://github.com/laravel/framework/pull/49853
[10.x] 修复密码最大值验证信息,
作者 @MrPunyapal 发布于 https://github.com/laravel/framework/pull/49861
[10.x]修复最大文件大小的验证信息,
作者 @mateusjunges 发布于 https://github.com/laravel/framework/pull/49879
更新 README.md
by @foremtehan in https://github.com/laravel/framework/pull/49878
[10.x] 添加 FormRequest[@getRules](https://github.com/getRules)() 方法
by @cosmastech in https://github.com/laravel/framework/pull/49860
[10.x] 添加 addGlobalScopes 方法,
作者 @emargareten 发布于 https://github.com/laravel/framework/pull/49880
[10.x] 允许砖/数学 0.12
by @LogicSatinn in https://github.com/laravel/framework/pull/49883
[10.x] 添加对流 JSON 响应的支持
by @pelmered in https://github.com/laravel/framework/pull/49873
[10.x] 在 LockableFile.php 中使用本地 fopen 异常
by @eusonlito in https://github.com/laravel/framework/pull/49895
[10.x] 测试工匠命令时修复 LazilyRefreshDatabase 问题,
作者 @iamgergo 发布于 https://github.com/laravel/framework/pull/49914
[10.x] 由 @tpetry 在 https://github.com/laravel/framework/pull/49912
中提出的修复 with-functions 做聚合时的表达式问题
[10.x] 修复缓存 ttl 超过时间时 redis 标签条目不会变旧的问题,
作者 @jagers,发布于 https://github.com/laravel/framework/pull/49864
[10.x] 修复 - 翻译器可能会错误地报告丢失的翻译键的地域,
作者 @VicGUTT,发布于 https://github.com/laravel/framework/pull/49900
[10.x]修复 Before/After 验证规则,
作者 @MrPunyapal,发布于 https://github.com/laravel/framework/pull/49871
转:
https://laravel-news.com/laravel-10-43-0
123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号