如何删除Laravel中的可变形数据

问题描述 投票:0回答:2

当我删除变形为该产品的产品图像时,不会被删除。

controller

public function destroy($id)
{
    $product = Product::findOrFail($id);
    if(!empty($product->image)){
        Storage::delete($product->image);
    }

    $images = Gallery::where('imageable_id', $product->id)->get(); //i get the images but it won't delete them
    if(!empty($images)){
        Storage::delete($images);
    }

    $product->delete();
    return redirect()->route('products.index');
}

任何想法?

php laravel
2个回答
1
投票

您不会从表中删除记录。仅从存储中删除它们。尝试这样的事情。

$product->image()->delete();

编辑

要从文件夹中删除文件,您可以执行以下操作。

    for($x = 0; $x < sizeof($images); $x++)
    {
       $file= $image->your_file_path;
       $filename = public_path().'/uploads_folder/'.$file;
       \File::delete($filename);
    }

完整代码,

$images = Gallery::where('imageable_id', $product->id)->get(); //i get the images but it won't delete them
if(!empty($images)){

for($x = 0; $x < sizeof($images); $x++)
 {
   $file= $image->your_file_path;
   $filename = public_path().'/uploads_folder/'.$file;
   \File::delete($filename);
 }

 $product->image()->delete();
}

0
投票

此简单代码将解决变形删除问题

$product->image()->delete();
© www.soinside.com 2019 - 2024. All rights reserved.