Introduction

In scenarios when you delete a parent record - say for example a blog post - you may want to also delete any comments associated with it as a form of self-maintenance of your data.

Normally, you could leverage foreign key constraints offered by your MySQL or Postgres database by adding an ON DELETE CASCADE rule to the foreign key constraint in your comments table.

In situations where you may find it useful to be able to restore a parent record after it was deleted, you may reach for Laravel's soft deleting functionality.

In doing so, however, you lose the ability to use the cascading delete functionality that your database would otherwise provide, as a soft delete is an application-level concept. That is where my new cascading soft deletes package aims to bridge the gap in functionality when using the SoftDeletes trait.

By using the CascadeSoftDeletes trait in combination with Eloquent's SoftDeletes trait, you can define any relationships -whether they themselves are soft deleted or not - to be deleted whenever your soft-deleted parent record is deleted.

<?php

namespace App;

use App\Comment;
use Iatstuti\Database\Support\CascadeSoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    use SoftDeletes, CascadeSoftDeletes;

    protected $cascadeDeletes = ['comments'];

    protected $dates = ['deleted_at'];

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}    

Now you can delete an App\Post record, and any associated App\Comment records will be deleted. If the App\Comment record implements the CascadeSoftDeletes trait as well, its children will also be deleted and so on.

$post = App\Post::find($postId)
$post->delete(); // Soft delete the post, which will also trigger the delete() method on any comments and their children.

It's important to know that when you cascade your soft deleted child records, there is no way to know which were deleted by the cascading operation, and which were deleted prior to that. This means that when you restore the blog post, the associated comments will not be restored as well.

Because this trait hooks into the deleting Eloquent model event, we can prevent the parent record from being deleted as well as any child records, if any exception is triggered. A LogicException will be triggered if the model does not use the Illuminate\Database\Eloquent\SoftDeletes trait, or if any of the defined cascadeDeletes relationships do not return an instance of Illuminate\Database\Eloquent\Relations\Relation.

Conclusion

I hope some of you will find some use for this trait in your models. If you are having general issues with this package, feel free to contact me on Twitter.

If you believe you have found an issue, please report it using the GitHub issue tracker, or better yet, fork the repository and submit a pull request.

If you're using this package, I'd love to hear your thoughts. Thanks!

Thanks to Adam Wathan for prompting me to finally put this package together, after his recent Full Stack Radio episode with Edd Mann, where the topic of soft deletes came up.