Skip to content

Archiving/Soft Deleting + it's effect on child relations #139

@jaredkove

Description

@jaredkove

What you would like to change/add

I would like to add that we opt to create default scopes to check parent relationships of rows, rather than archiving child items of an archived/soft deleted record. This only if the child is dependant on the parent and can't be in the system without it.

👍 For yes
👎 For no

Why you would like to change/add this

I would like to do this for the following reasons;

  • Soft deleting child items means you have to remember to restore them across the site.
  • If the child item is a child item of multiple entities, you wouldn't know which one forced the deletion and therefore whether to restore it
  • You would need to check timestamps of the child item soft deletion relative to the parent object deletion timestamp when restoring to define whether you need to actually restore it or whether it itself was soft deleted.
  • You also then are forced to add soft delete functionality (at least the trait, relative changes to queries) to all child entities when actually you might not need to soft delete them as part of the application.

This adds more of a risk of developers not understanding the above concept about the advanced aspects of restoring a record + potentially loses that data of when the child was soft deleted, versus what I think is simple.

Examples

In My Way:

// In the model or whatever (you wouldn't need overrides, actually..)
$this->delete();
// In the child entity

/**
 * The "booting" method of the model.
 *
 * @return void
 */
protected static function boot()
{
    parent::boot();

    static::addGlobalScope('activeParent', function (Builder $builder) {
        $builder->whereHas('parentRelationship');
    });
}

In the other way people do it currently:

// In the model override for delete
$this->delete();
$this->childrenItems()->delete();
$this->otherChildRelationship()->delete();
// In the restore function
$datetime = $this->deleted_at;
$this->restore();
$this->childrenItems()->withTrashed()->whereDate('deleted_at', '>=', $datetime)->restore();
$this->otherChildRelationship()->withTrashed()->whereDate('deleted_at', '>=', $datetime)->restore();

Checklist

  • I have checked that this is not a duplicate issue.

Metadata

Metadata

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions