use Illuminate\Support\Facades\File;
use Intervention\Image\Facades\Image;
-------
if (request('image')) {
// delete the old image file
if (File::exists('backend/img/categories/main/' . $mainCat->image)) {
File::delete('backend/img/categories/main/' . $mainCat->image);
}
// save the new image file
$image = request()->file('image');
$img = rand() . '.' . $image->getClientOriginalExtension();
$location = public_path('backend/img/categories/main/' . $img);
$imageResize = Image::make($image);
$imageResize->fit(300, 300)->save($location);
// update the category record with the new image filename
$mainCat->image = $img;
}
$mainCat->save();
The image is saving but it cant for some reason read my old image and delete it to save new image rather its creating a new image leaving the old image in that folder. Did the same for destroy function and weirdly its working there.