Doing something before delete in Eloquent Models Even in mass deleting

Posted 2 months ago by m.f.rasoulnejad

hi. I have a model like 'Product' it has a field called 'photo'. I want to delete its photo file from my disk every time i delete a product from too. this is a really simple task like so:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
    public function delete()
    {
    \File::delete('assets/images/products/'.$this->photo);
        return parent::delete();
    }
}

But when I mass delete these products like this:

$products= Product::whereRaw('FIND_IN_SET(?,category)', [$someCategoryObject->id]);
$products->delete();

its not deleting the photo file of these products. whats wrong? I'm new in laravel and this made me angry for a whole freaking day!! haha.

Please sign in or create an account to participate in this conversation.