Skip to main content
Back to packages
1,175 GitHub stars

michaeldyrynda/laravel-cascade-soft-deletes

View on GitHub

Description

Cascading soft deletes for the Laravel PHP Framework.

<?php
 
namespace App;
 
use App\Comment;
use Dyrynda\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'];
 
protected $fetchMethod = 'get'; // get, cursor, lazy or chunk
// protected $chunkSize = 500;
 
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, it's 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.

Related Content on Laravel Daily

Video
Video

Recent Courses on Laravel Daily

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.