我正在尝试更新产品表中的数据。但是,当我尝试更新laravel时,它给了我一个错误,提示在数组上调用成员函数fill()。当我调试$ data时,得到了一组键和值。
我有以下代码:
public function update(Request $request,Products $product)
{
$this->products = $this->products->find($product->id);
if(!$this->products){
request()->session()->flash('error','Product not found!');
return redirect()->route('product.index');
}
$rules = $this->products->getRules('update');
$this->products = $request->validate($rules);
$data = $request->all();
$data['added_by'] = $request->user()->id;
$data['image'] = explode(',',$data['related_images'])[0];
$this->products->fill($data); //Getting error here
$status = $this->products->save();
if($status){
$request->session()->flash('success','Product updated successfully.');
} else {
$request->session()->flash('error','Sorry! there was problem updating product.');
}
return redirect()->route('product.index');
}
我希望fill()函数起作用,以便它可以更新产品表上的数据。
答案 0 :(得分:0)
下面的行用通过验证的密钥数组覆盖products
。您可以通过删除分配来解决此问题(如果有错误,仍然会返回422错误)
$this->products = $request->validate($rules);
// change to
$request->validate($rules);