-1

I can't add images to the database. I am always getting this error.

    $this->validate($request, [
        'imageName' => 'required',
        'imagePath' => 'required',
    ]);
    $image->productId = $request->input('productId');
    $image->imageName = $request->input('imageName');

    if (request()->hasFile('imagePath')){
        $image = $request->file('imagePath');
        $imageName = time() . '.' . $image->getClientOriginalExtension();
        $destinationPath = public_path('/images/productImages/');
        $image->move($destinationPath, $imageName);
        $image->imagePath = $destinationPath . $imageName;
    }
    $image->imageCode = $request->input('imageCode');
    $image->save(); // error line

    return redirect()->route('image.index');

Thank you for your help

Mustafa BATMAZ
  • 11
  • 1
  • 1
  • 2

1 Answers1

1

Actually from your code what I can understand is that you have an eloquent object and you want to upload an image and save the image data to eloquent object,

but inside your if (request()->hasFile('imagePath')){ } you have overwritten your $image variable :)

In that case change your code inside if conditional to the following

 if (request()->hasFile('imagePath')){
        $uploadedImage = $request->file('imagePath');
        $imageName = time() . '.' . $image->getClientOriginalExtension();
        $destinationPath = public_path('/images/productImages/');
        $uploadedImage->move($destinationPath, $imageName);
        $image->imagePath = $destinationPath . $imageName;
    }
Shobi
  • 10,374
  • 6
  • 46
  • 82