Level 122
you are trying to get the image path AFTER you deleted the model.
In adition, you are treating the boolean that comes from the delete as the model
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am getting this error Attempt to read property \"product_image\" on bool when i try to delete image from the database and also delete it from my storage folder. My api code is able to delete the image from the database but is not able to delete the image in the storage folder.
ProductController.php file
public function addProductImage(Request $request, $id)
{
$fields = $request->validate([
'product_image' => 'required',
]);
$products = Product::where(['id' => $id])->first();
if ($request->hasFile('product_image')) {
$images = $request->file('product_image');
foreach($images as $image){
$image_product = new ProductImage;
$this->productImage = $image->store('product/show/' . Str::random(), 'public');
$image_product->product_image = $this->productImage;
$image_product->product_id = $id;
$image_product->save();
}
}
return response()->json([
"image_product"=>$image_product,
"message"=>"Product Image Added Successfully"
]);
}
public function deleteProductImage($id)
{
$product_image = ProductImage::findorfail($id)->delete();
if ($product_image->product_image) {
Storage::disk('public')->deleteDirectory(dirname($product_image->product_image));
}
return [
'message' => 'Product Image Deleted Successfully'
];
}```
and the error is indicated @ the ```if ($product_image->product_image)``` Any help?
public function deleteProductImage($id)
{
$product_image = ProductImage::findorfail($id);
if (!empty($product_image->product_image)) {
Storage::disk('public')->deleteDirectory(dirname($product_image->product_image));
}
$product_image->delete();
return [
'message' => 'Product Image Deleted Successfully'
];
}
Please or to participate in this conversation.