Laravel 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.
我在哪里错了?
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.
下方的存储映像位于例如-storage/app/public/images/products/4/image.png
Under storage images are located at e.g. - storage/app/public/images/products/4/image.png
在公共场合-public/storage/images/products/4/imagep.png
Under public - public/storage/images/products/4/imagep.png
图像路径在数据库中存储为-/storage/images/products/4/ACTCMRcYlWR8Bn3ZxoJ7bpiDJ7.png
Image path is stored in database as - /storage/images/products/4/ACTCMRcYlWR8Bn3ZxoJ7bpiDJ7.png
推荐答案
我必须在路径中添加删除"/storage"部分,并在其前添加"/public".
I had to add remove '/storage' part of the path and prepend '/public'.
$ productImage = str_replace('/storage','',$ product-> image_path);
Storage :: delete('/public'.$ productImage);
这篇关于Laravel 5.3:从存储中删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!