Laravel 5.3: delete image from storage

六眼飞鱼酱① 提交于 2020-08-27 21:58:53

问题


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


回答1:


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

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

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



来源:https://stackoverflow.com/questions/43706241/laravel-5-3-delete-image-from-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!