I have a method which deletes a product and image that belongs to it, but I can not delete the image.
Where am I going wrong with this?
public function deleteProduct($ids)
{
foreach ($ids as $id => $value) {
$product = Product::find($id);
$productImage = $product->image_path;
if ($productImage) {
Storage::delete($productImage);
$product->destroy($id);
}
}
}
I have a symlink between storage and public folder.
Under storage images are located at e.g. - storage/app/public/images/products/4/image.png
Under public - public/storage/images/products/4/imagep.png
Image path is stored in database as - /storage/images/products/4/ACTCMRcYlWR8Bn3ZxoJ7bpiDJ7.png
One way to delete a file from the public directory in Laravel is to use the Storage facade. To delete a file, you will need to follow the following steps: Step 1: Check to ensure that the folder and file exist. Step 2: Delete the required file.
To delete a directory in the Laravel application you can make use of the "deleteDirectory()" method available from the Storage facade. <? php Storage::deleteDirectory($directory); This method will delete the directory with all of its files so you must carefully ensure that you are deleting the right directory.
use Illuminate\Support\Facades\Storage; $images = explode(",", $vehicles->images); Storage::delete($iamges); Both of these examples can be found in the Laravel docs. Yes, your code still adds images to the uploaded-images folder. First you are deleting the existing images, then you are adding the new ones.
I had to add remove '/storage' part of the path and prepend '/public'.
$productImage = str_replace('/storage', '', $product->image_path);
Storage::delete('/public' . $productImage);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With