In this video we are going to see the Problem of Product Image Deletion and Their Solution.
So first of all let see the problem.
Lets login with admin credential.
Now go to the products page.
And here lets delete any product .
From here before deleting the product lets check the image in project directory.
So first of lets get the product image name.
So go to the phpMyAdmin.
And from here lets open the database.
And then browser the products table.
And here you can see the product now lets scroll this.
And here is the product image name.
Now lets copy this name.
And go to the project directory then public/assets/images/products.
Now search here so paste here the product image name and hit enter And here you see the product image.
Now lets delete the product.
So go to the product page. and now click on delete.
And here you can see product has been deleted.
Now lets check the product image.
So here you see this product image is still exist.
It means that the product has been removed but the image of the product has not been removed.
So to delete the product image while deleting the product.
Lets add some code inside the deleteProduct method.
So go to the AdminProductComponent.php Class file and make the following changes.

public function deleteProduct($id)
{
$product = Product::find($id);
if($product->image)
{
unlink('assets/images/products'.'/'.$product->image);
}
if($product->images)
{
$images = explode(\",\",$product->images);
foreach($images as $image)
{
if($image)
{
unlink('assets/images/products'.'/'.$image);
}
}
}
$product->delete();
session()->flash('message','Product has been deleted successfully!');
}


That’s it now save this.
And lets check again.
And now refresh the page.
And this time now lets delete this product.
Which image name is lets see this one so lets copy.
And go to the product directory.
And search here.
And you can see the image.
Now delete the product and product deleted now lets check the image in product directory.
And here you see product image is no more alright.
So in this way you can solve this image deletion problem.