Creating default object from empty value in Laravel 8

946 Views Asked by At

For some reason, I still get a warning message: Creating default object from empty value.

Product controller

public function update(Request $request, $id)
{
    $products = new Product();
    $products = Product::find($id);

    if ($request->hasFile('image'))
    {
        $path = 'assets/uploads/products/'.$products->image;
        if (File::exists($path))
        {
            File::delete($path);
        }

        $file = $request->file('image');
        $ext = $file->getClientOriginalExtension();
        $filename = time().'.'.$ext;
        $file->move('assets/uploads/products/',$filename);
        $products->image = $filename;
    }

    $products->name = $request->input('name');
    $products->slug = $request->input('slug');
    $products->small_description = $request->input('small_description');
    $products->description = $request->input('description');
    $products->origanl_price = $request->input('origanl_price');
    $products->selling_price = $request->input('selling_price');
    $products->qty = $request->input('qty');
    $products->tax = $request->input('tax');
    $products->status = $request->input('status') == TRUE? '1':'0';
    $products->trending = $request->input('trending') == TRUE? '1':'0';
    $products->meta_title = $request->input('meta_title');
    $products->meta_keywords = $request->input('meta_keywords');
    $products->meta_description = $request->input('meta_description');
    $products->update();

    return redirect('/products')
        ->with('success', 'Product updated Successfully');
}

When I update the product, the page returns an error.

Creating default object from empty value.

How can I properly initialize the new empty object?

0

There are 0 best solutions below