我的 API 遇到这个问题。 我不更新数据库中的记录。 在 Postman 中,响应是 true,但不会保存在数据库中。 在 Postman 中,我使用 PUT 方法传递并在正文名称中设置文本。
产品控制器:
public function update(Request $request, $id)
{
$product = auth()->user()->products()->find($id);
if (!$product) {
return response()->json([
'success' => false,
'message' => 'Product with id ' . $id . ' not found'
], 400);
}
$updated = $product->fill($request->all())->save();
if ($updated)
return response()->json([
'success' => true
]);
else
return response()->json([
'success' => false,
'message' => 'Product could not be updated'
], 500);
}
您应该查看您的
Product
模型,看看名称是否设置为可填写字段:$fillable = ['name'];
。另外,关键可能只是name
而不是"name"
。