【问题标题】:Call to a member function fill() on array调用数组上的成员函数 fill()
【发布时间】:2019-07-27 12:29:03
【问题描述】:

我正在尝试更新产品表中的数据。但是,当我尝试更新 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() 函数能够工作,以便它可以更新我在产品表上的数据。

【问题讨论】:

    标签: laravel


    【解决方案1】:

    下面的行用通过验证的键数组覆盖您的products。可以通过移除赋值来修复(如果有错误,仍然会返回422错误)

    $this->products = $request->validate($rules);
    // change to
    $request->validate($rules);
    

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多