The findOrFail
method return only one item. So you cannot iterate over it.
You can use the each
method
$product = Products::findOrFail($id);
$images = $product->images()->get();
$images->each(function ($file, $key) {
$filePath = public_path("images2/") . $file->filename;
File::delete($filePath);
});
// Delete the product
$product->delete();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…