侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Laravel 10.43版本发布中添加流JSON响应

Laravel  /  管理员 发布于 1年前   352

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

  • 上一条:
    使用 Laragenie 通过 CLI 提出有关代码库的人工智能问题
    下一条:
    在go语言中实现阿克曼函数示例
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • Laravel 11.14版本发布 - 新的字符串助手和ServeCommand改进(0个评论)
    • Laravel 11.12版本发布 - Artisan的`make`命令自动剪切`.php `扩展(0个评论)
    • Laravel的轻量型购物车扩展包:binafy/laravel-cart(0个评论)
    • Laravel 11.11版本发布 - 查看模型中的第三方关系:show(0个评论)
    • 近期文章
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-01
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客