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



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

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');
}

知道吗

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

$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();
}

这个简单的代码将解决变形删除问题

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

最新更新