我有一个laravel项目,位于laravel 5.5。我手动创建了一个额外的字段" uploaded_image"在现有的"产品"表。当我将数据传递到laravel控制器(ProductController.php)时,除了新创建的字段外,所有现有字段都会更新。下面是我的ProductController代码:
public function update(Request $request, $id)
{
$data = $request->validate([
'product_name' => 'required',
'description' => 'required',
'rating' => 'required'
]);
$uploaded_image = $request->input('uploaded_image');
$data['uploaded_image'] = $uploaded_image['filename']; // which is abc.png
$product = Product::findOrFail($id);
$product->update($data);
return response()->json($data);
}
答案 0 :(得分:2)
在uploaded_image
模型Product
数组
fillable
protected $fillable = [
'uploaded_image'
];
答案 1 :(得分:0)
通过将文件名作为字符串
发送来更改代码$uploaded_image = $request->input('uploaded_image');
$data['uploaded_image'] = $uploaded_image; // which is abc.png
$product = Product::findOrFail($id);
$product->update($data);
return response()->json($data);
或者如果您希望文件本身发送
$uploaded_image = $request->file('uploaded_image');
$data['uploaded_image'] = $uploaded_image->getFilename(); // which is abc.png
$product = Product::findOrFail($id);
$product->update($data);
return response()->json($data);