Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

an.leclerc's avatar

Laravel soft deleting with related table

Hi,

I'd like to delete entities and related tables.

I tried with event :

/**
     * Handle the DossierFinancingPlanModel "deleting" event.
     */
    public function deleting(DossierFinancingPlanModel $dossierFinancingPlanModel): void
    {
        $dossierFinancingPlanModel->dossierFinancingPlanCalculated()->delete();
        $dossierFinancingPlanModel->dossierFinancingPlanCharges()->delete();
        $dossierFinancingPlanModel->dossierFinancingPlanDebts()->delete();
        $dossierFinancingPlanModel->dossierFinancingPlanLoans()->delete();
        $dossierFinancingPlanModel->dossierFinancingPlanNeeds()->delete();
        $dossierFinancingPlanModel->dossierFinancingPlanProperties()->delete();
        $dossierFinancingPlanModel->dossierFinancingPlanResources()->delete();
    }

But a I this error :

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`hub.wineo.io`.`dossier_financing_plan_calculated`, CONSTRAINT `dfpca_dfp_id` FOREIGN KEY (`dossier_financing_plan_id`) REFERENCES `dossier_financing_plan` (`id`)) (Connection: mysql, SQL: delete from `dossier_financing_plan` where `id` = 5f92b813-a32e-484e-a3f3-ee57990b1e52)

I don't understand why, "deleting" event means "DossierFinancingPlanModel" not deleted yet ?!

0 likes
7 replies
Glukinho's avatar

dossier_financing_plan_calculated references to dossier_financing_plan table with delete constraint, so you can't delete a row from dossier_financing_plan_calculated without previously deleting corresponding row from dossier_financing_plan.

Why you mentioned soft delete? Is it turned on for your models?

Glukinho's avatar

@an.leclerc If soft delete is really turned on, there should be no SQL DELETE on deleting a model. Check that all models have use SoftDeletes trait. I think you forgot it on some models and they try to perform actual SQL DELETE instead of setting deleted_at field.

Please or to participate in this conversation.