joggapp / laravel-cascade-soft-deletes
Cascading soft deletes for the Laravel PHP Framework
Installs: 2 729
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- illuminate/support: ^11.0
Requires (Dev)
- orchestra/testbench: ^9.0
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2024-11-13 10:08:40 UTC
README
Implementation
- Add the
JoggApp\LaravelCascadeSoftDeletes\Traits\CascadeSoftDeletes
trait to parents models. - Set
protected $cascadeSoftDeletes
to an array of child relationships for soft deletes to cascade down to.
Code Samples
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use JoggApp\LaravelCascadeSoftDeletes\Traits\CascadeSoftDeletes; class User extends Model { use SoftDeletes, CascadeSoftDeletes; protected $cascadeSoftDeletes = [ 'posts', ]; public function posts() { return $this->hasMany(Post::class); } }
Now you can delete an App\Models\User
record, and any associated App\Models\Post
records will be deleted. If the App\Models\Post
record implements the CascadeSoftDeletes
trait as well, it's children will also be deleted and so on.
$user = App\Models\User::find($userId); $user->delete(); // Soft delete the user, which will also trigger the delete() method on any posts and their children.