php – update()后返回集合?

使用Raw,如何返回更新行的集合?

例如:

$updated = DB::table('users')->where('id', 1)->update(['votes' => 123]);

我期待dd($updated)返回更新的集合行,但它返回1.

{{$updated->votes}} should return 123

最佳答案 这不是它的工作原理.您不能指望此查询会返回一个对象:

$updated = DB::table('users')->where('id', 1)->update(['votes' => 123]);

如果您只想按照问题中提到的那样使用查询生成器,则需要手动获取对象:

$data = DB::table('users')->where('id', 1)->first();

使用Eloquent,您可以使用updateOrCreate():

$data = User::where('id', 1)->updateOrCreate(['votes' => 123]);

这将返回一个对象. update()将返回boolean,因此您无法在此处使用它.

点赞