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] Practical Laravel Security: Packages, Secrets, Supply-Chain Attacks
7 lessons
43 min read
Roles and Permissions in Laravel 13
14 lessons
57 min
How to Build Laravel 13 API From Scratch
30 lessons
1 h 23 min