Search code examples
laravellaravel-5.3

Laravel 5.3: delete image from storage


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


Solution

  • I had to add remove '/storage' part of the path and prepend '/public'.

    $productImage = str_replace('/storage', '', $product->image_path);

    Storage::delete('/public' . $productImage);