0
votes

I am trying to update data from the product table. But, when I try to update laravel, it gives me an error which says call to a member function fill() on array. When I debug $data I got an array of key and values.

I have the following code:

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');
    }

I want the fill() function to work so that it can update my data on the product table.

1

1 Answers

0
votes

The line below is overwriting your products with an array of the keys that passed validation. You can fix it by removing the assignment (if there are errors, a 422 error will still be returned)

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