Laravel 5.5 - 特定字段未插入

时间:2018-04-05 08:34:18

标签: laravel laravel-5

我有一个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);
}

2 个答案:

答案 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);