hadis
hadis

Reputation: 39

update and edit image issue in laravel

I want to edit image but I have this error and I know it's because of the saveing image path as string but I don't know how to fix it

 public function update(Request $request,$id)
    {
       $data = Singleproduct::find($id);
        $data->name = $request->name;
        $data->explain = $request->explain;
        $data->price = $request->price;
        $data->parent_name = $request->parent_name;

        $data = $request->image.$request->image->getClientOriginalExtension();

        $data->save();

        return redirect(route('product.index'));
    }

the error is "Call to a member function save() on string" can anyone help me to fix it?

Upvotes: 0

Views: 75

Answers (1)

Mohammad Naji
Mohammad Naji

Reputation: 5442

If Singleproduct model has a property image, which is a string containing image's filename (like 'my-product.jpg'), you may want to...

$data->image = $request->image->getClientOriginalName();

And then $data->save() is expected to work.

Anyway I suggest you use better naming for your Model and variables.

If Singleproduct is a product, I prefer Product name for the model.

Also if I were to write the code, maybe I would do it like so...

public function update(ProductUpdateRequest $request, $id)
{
    $product = Product::findOrFail($id);
    $product->name = $request->name;
    $product->explain = $request->explain;
    $product->price = $request->price;
    $product->parent_name = $request->parent_name;
    $product->image_filename = $request->image->getClientOriginalName();

    $product->save();

    return redirect(route('product.index'));
}

Upvotes: 1

Related Questions