There are many related questions but unfortunately I can't find working solution
I have Laravel model and when this model is deleted I want to
- delete some related models
- run custom SQL query when deleting model
My Laravel's model class looks like (as you can see models can have different relation types)
class ModelA extends Model
{
public functions modelsB() {
return $this->hasMany(ModelB:class);
}
public functions modelsC() {
return $this->belongsToMany(ModelC:class);
}
// other related models
// place where I am expecting actual deleting is happening
public static function boot() {
parent::boot();
self::deleting(function($modelA) {
$modelA->modelsB()->get()->each->delete();
foreach($modelA->modelsC as $modelC){
$modelC->delete();
}
});
}
}
ModelA is deleted but all related data stays, and I am not sure that it is even being called. Maybe I missed something? Should I extend some class for my ModelA? Or this boot function should be placed somewhere else?