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.
1<?php 2 3namespace App; 4 5use App\Comment; 6use Iatstuti\Database\Support\CascadeSoftDeletes; 7use Illuminate\Database\Eloquent\Model; 8use Illuminate\Database\Eloquent\SoftDeletes; 9 10class Post extends Model11{12 use SoftDeletes, CascadeSoftDeletes;13 14 protected $cascadeDeletes = ['comments'];15 16 protected $dates = ['deleted_at'];17 18 public function comments()19 {20 return $this->hasMany(Comment::class);21 }22}
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.
1$post = App\Post::find($postId)2$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.