使用laravel eloquent update时,
查询已执行,但它没有成功响应,它说call to a member function save() on int in file .... line 43
我的更新函数如下所示:
public function removebookingbyid(Request $request) {
$toUpdateColumns = ['is_delete' => 1, 'updated_at' => Carbon::now()];
$data = InstructorSchedule::where('id', $request->booking_id)
->update($toUpdateColumns);
$res = $data->save();
if ($res) {
return response()->json(['status'=>1,'msg'=>'ok'], 200);
} else {
return response()->json(['status'=>0,'msg'=>'bad'], 500);
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个与此几乎相同的功能,不同之处在于我只有 1 列要更新,并且运行良好。但我不知道为什么这个会给我这样的错误。
小智 5
来自 Laravel 文档: https: //laravel.com/docs/8.x/eloquent#updates
当通过 Eloquent 发出批量更新时,更新后的模型不会触发保存、保存、更新和更新模型事件。
这是因为在发布批量更新时从未实际检索模型。
也许这样的事情会起作用:
$res = InstructorSchedule::findOrFail($request->booking_id);
$res->fill($toUpdateColumns);
$res->save();
Run Code Online (Sandbox Code Playgroud)