Cascade Delete & Restore when using SoftDeletes
William shows us how to use Laravel Soft Delete functionality to retain deleted records in our database.
Laravels Soft Deleting allows you to retain deleted records in your database so they can be used or restored at a later point. Restoring deleted records is great if a mistake is made and you need to revert and retaining deleted data can often be useful in the event you need to pull up some old records.
Traditionally Deleting Relations
I've always used MySQL foreign key constraints to cascade delete related records. Laravel makes it easy to use foreign keys in migrations, set
onDeleteto cascade and walla, your relations will be deleted automatically.
But what happens when you enable SoftDeletes? Your database is never told to actually 'delete' a record, instead the
deleted_atfield is updated. So what happens to your cascading deletes? Nothing, your related records are left alone.
I've created laravel5-soft-cascade to help make cascading soft deletes and restores as simple as writing an array.
This post is submitted by one of our members. You may submit a new post here.