Return Collection after update()?(update() 后返回集合?)
问题描述
使用 Raw,如何返回更新行的集合?
Using Raw, how to return collection of updated row?
例如:
$updated = DB::table('users')->where('id', 1)->update(['votes' => 123]);
我期待 dd($updated)
返回更新后的集合行,但它返回了 1.
I was expecting dd($updated)
to return updated row of collection but it returned 1.
{{$updated->votes}} should return 123
推荐答案
它不是这样工作的.你不能指望这个查询会返回一个对象:
That's not how it works. You can't expect this query will return you an object:
$updated = DB::table('users')->where('id', 1)->update(['votes' => 123]);
如果您只想按照问题中提到的方式使用查询生成器,则需要手动获取一个对象:
If you want to use Query Builder only as you mentioned in your question, you'll need to get an object manually:
$data = DB::table('users')->where('id', 1)->first();
使用 Eloquent,您可以使用 updateOrCreate()
:
With Eloquent you can use the updateOrCreate()
:
$data = User::where('id', 1)->updateOrCreate(['votes' => 123]);
这将返回一个对象.update()
将返回布尔值,因此您不能在这里使用它.
This will return an object. update()
will return boolean, so you can't use it here.
这篇关于update() 后返回集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:update() 后返回集合?


基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01