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
[NEW] Laravel Boost: Deeper Practical Dive
8 lessons
32 min
NativePHP Mobile v4 with SuperNative: Mini-Course
5 lessons
18 min
Next.js Basics for Laravel Developers
11 lessons
58 min