laraveleloquentrelationshipeloquent-relationship

how to delete all eloquent child one by one. its hierarchy like category => course => chapter => exams


I have one issue when i delete parent i want to delete all child of from different modal.i want when i delete category delete all course of category and delete all chapter of course and delete all exams of chapter

I have 4 Models [ Category,Course,Chapter,Exam ]

category child is course course child is chapter chapter child is exams

//Category modal 

public function courses()
{
    return $this->hasMany(Course::class);
}

// Course Model 

public function category()
{
    return $this->belongsTo(Category::class);
}


public function chapters()
{
    return $this->hasMany(CourseChapter::class);
}

// Chapter model

public function course()
{
    return $this->belongsTo(Course::class);
}

public function exams()
{
    return $this->hasMany(Exam::class);
}

Solution

  • There are two ways you can do it:

    1 - The Migration Way

    Inside each of your migrations where you have a foreign key write it like this:

    Courses Migration:

    $table->foreignId('category_id)->constrained()->cascadeOnDelete();
    

    This will make sure as soon as a category is deleted, all the related courses are deleted. Repeat the same process for rest of them like:

    // chapter
    $table->foreignId('course_id')->constrained()->cascadeOnDelete();
    
    // exam
    $table->foreignId('chapter_id')->constrained()->cascadeOnDelete();
    

    2 - The Model Way

    You will need to implement Laravel's cascading delete feature inside each model, here is how you have can implement this:

    // Category model
    class Category extends Model
    {
        public function courses()
        {
            return $this->hasMany(Course::class);
        }
    
        protected static function boot()
        {
            parent::boot();
    
            static::deleting(function ($category) {
                $category->courses()->delete();
            });
        }
    }
    
    // Course model
    class Course extends Model
    {
        public function category()
        {
            return $this->belongsTo(Category::class);
        }
    
        public function chapters()
        {
            return $this->hasMany(Chapter::class);
        }
    
        protected static function boot()
        {
            parent::boot();
    
            static::deleting(function ($course) {
                $course->chapters()->delete();
            });
        }
    }
    
    // Chapter model
    class Chapter extends Model
    {
        public function course()
        {
            return $this->belongsTo(Course::class);
        }
    
        public function exams()
        {
            return $this->hasMany(Exam::class);
        }
    
        protected static function boot()
        {
            parent::boot();
    
            static::deleting(function ($chapter) {
                $chapter->exams()->delete();
            });
        }
    }
    
    // Exam model
    class Exam extends Model
    {
        public function chapter()
        {
            return $this->belongsTo(Chapter::class);
        }
    }