Video
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
Video
Recent Courses on Laravel Daily
Testing in Laravel 13 For Beginners
26 lessons
1 h 41 min read
Laravel 13 Eloquent: Expert Level
41 lessons
1 h 34 min
Queues in Laravel 13
18 lessons
1 h 12 min read