laravel 8 image update process
$category = Category::find($id);
if($request->hasFile('image'))
{
$path = 'assets/uploads/category/'.$category->image;
if(File::exists($path))
{
File::delete($path);
}
$file = $request->file('image');
$ext = $file->getClientOriginalExtension();
$filename = time().'.'.$ext;
$file->move('assets/uploads/category/',$filename);
$category->image = $filename;
}
//Another way image update process
//Handle Main image
if ($request->hasFile('product_thumbnail')) {
// Delete old product thumbnail if exists
if ($product->product_thumbnail) {
$imagePath = public_path('images/' . $product->product_thumbnail);
if (file_exists($imagePath)) {
unlink($imagePath); // Delete the file from the filesystem
}
}
// Upload new product thumbnail
$imageName = time().'.'.$request->product_thumbnail->extension();
$request->product_thumbnail->move(public_path('images'), $imageName);
$product->product_thumbnail = $imageName;
}
No comments