Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting related models one-to-many in Laravel

Is this even possible? In every tutorial that I read there is nothing about deleting, only selecting and inserting related models.

This is my problem:

I have three levels of nesting. I have classes Package, Level, Lesson and Phase, and their models are below. First one - packages:

class Package extends Eloquent  {

protected $table = 'packages';
public $timestamps = false;

public function levels(){
    return $this->hasMany('Level', 'package_id');
}

}

Levels:

class Level extends Eloquent  {

protected $table = 'levels';

public function lessons(){
    return $this->hasMany('Lesson', 'level_id');
}

public function package(){
    return $this->belongsTo('Package', 'package_id');
}

}

Lessons:

class Lesson extends Eloquent {

protected $table = 'lessons';

public function phases(){
    return $this->hasMany('Phase', 'lesson_id');
}

public function level(){
    return $this->belongsTo('Level', 'level_id');
}

}

What I'm trying to do here is to when deleting one package i delete all levels related to it and also to delete all lessons related to those levels.

I have tried couple of options and they were all wrong, I just don't know how to do this without making a bunch of queries in foreach loop. Please, give some advice I'm so desperate I'm thinking about tweet to Jeffrey Way and ask him for the solution :) Thanks in advance

like image 545
Nikola Sicevic Avatar asked Dec 03 '14 14:12

Nikola Sicevic


People also ask

How do you delete many relationships in laravel?

To delete a model directly, call delete() on it and don't define a deleting listener in its boot method or define an empty deleting method. If you want to further delete relations of a related model, you will define a deleting listener in the boot method of that model and delete the relations there.

What is Cascade delete in laravel?

Laravel Soft Cascade is a package that makes it easy to perform soft cascade deletes and restores on related models using soft deleting.

How do I remove all data from a model in laravel?

In the first example, we are going to use the truncate() function, which is used to delete all the records. In the second example, we will delete records on the basis of the id. So we will specify some id and use the delete() function to delete all the records of that particular id.


1 Answers

You can approach this in two ways:

  1. Leverage the database to do the deleting for you. You'd add something like this to your migrations for the lessons, levels and packages, respectively:

     $table->foreign('level_id')->references('id')->on('levels')->onDelete('cascade');
    
     $table->foreign('lesson_id')->references('id')->on('lessons')->onDelete('cascade');
    
     $table->foreign('package_id')->references('id')->on('packages')->onDelete('cascade');
    
  2. You can overwrite the delete method on each model to delete all of its relationships:

    class Lesson extends Eloquent {
    
        protected $table = 'lessons';
    
        public function phases(){
            return $this->hasMany('Phase', 'lesson_id');
        }
    
        public function level(){
            return $this->belongsTo('Level', 'level_id');
        }
    
        public function delete()    
        {
            DB::transaction(function() 
            {
                $this->level()->delete();
                $this->phases()->delete();
                parent::delete();
            });
        }
    
    }
    
like image 78
edpaez Avatar answered Sep 29 '22 21:09

edpaez