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

skreng's avatar

Laravel Soft Delete Cascade

Hi!

It is possible delete second level children on cascade by soft Delete?

I have structure:

Company
    |_Domain
        |_Projects
            |_Tasks

When I Delete Company everything lower will also be Deleted. now When I delete Company only first level children is delete (Domain).

0 likes
10 replies
wilburpowery's avatar

I would assign a foreign key constraint on your migrations for domains, projects, and tasks. Like this:

$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');

You can also use Model events for this. On the Company model's boot method, listen for Eloquent's deleting event and simply use the relationships to delete all domains, projects, and tasks.

skreng's avatar

Everywhere I have onDelete('cascade')

Snapey's avatar
Snapey
Best Answer
Level 122

To soft delete relations, you have to do it using model observers

So if company is soft deleted, the 'deleting' event in the company model is hooked to also soft delete the domain(s) associated with company, and then in the Domain model, hook the deleting event so that projects are soft deleted, and so on down the chain of relations.

This is from L5.2; where CampaignEvent has Shifts and Shifts has Activities

CampaignEvent.php;

    //observe this model being deleted and delete the child shifts
    public static function boot ()
    {
        parent::boot();

        self::deleting(function (CampaignEvent $event) {

            foreach ($event->shifts as $shift)
            {
                $shift->delete();
            }
        });
    }

Shift.php

    //observe this model being deleted and delete the child activities
    public static function boot ()
    {
        parent::boot();

        self::deleting(function (Shift $shift) {

            foreach ($shift->activities as $activity)
            {
                $activity->delete();
            }
        });
    }

5 likes
aalaap's avatar

Foreign key constraints have portability problems, so I would advise against them. I wouldn't even go with the events and observers route, because that still feels like manual work. Instead, you could look at packages that add cascade deleting to your models via traits. You can use the traits with all models with children.

2 likes
Snapey's avatar

@aalaap Wow, you found a package to replace 8 lines of code.

11 likes
hoyvoy's avatar

With this package you have Eloquent/Query Builder support:

https://github.com/Askedio/laravel5-soft-cascade

Also support restricted relations, rollback if supported all actions when error occurs and support all relation types:

One To One

One To Many

One To Many (Inverse)

Many To Many

Has Many Through

Polymorphic Relations

Many To Many Polymorphic Relations

aalaap's avatar

@Snapey Not just 8 when you have thousands of models!

Using a trait is also cleaner and more expressive when you look at the model. It's also far less prone to bugs. Also... DRY!

4 likes
Sistema's avatar

Did anybody see the new Laravel's included functions to perform cascading softdeletes? Can't find it in docs but I'm pretty sure I have seen it somewhere.. I made a trait but I'm curious about their way to assolve it

donphelix's avatar

@Sistema Could you please expound on this, I have spent considerable amount of time trying to get a trait to solve this. I don't want to use these provided package since I want to own my code not being restricted later on dependencies update.

JerryBels's avatar

@donphelix Well since those packages are open source, simply look at how they do things to implement your solution... Or fork one and voila, you're 100% responsible of maintaining it, it's "your own code".

Please or to participate in this conversation.