在update()之后返回Collection吗? [英] Return Collection after update()?
本文介绍了在update()之后返回Collection吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用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()之后返回Collection吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文