title: Laravel Eloquent ORM - removing rows and all the child relationship, with event deleting
body: <p>I have three models that relate to each other one to many:</p>
  
  <h2>Country</h2>
  
  <pre><code>class Country extends Model
  {
      protected $fillable=['name','sort'];
      public $timestamps=false;
  
      public function region(){
          return $this-&gt;hasMany('App\Models\Region');
      }
  }
  </code></pre>
  
  <h2>Region</h2>
  
  <pre><code>class Region extends Model
  {
  
      protected $fillable=['country_id','name','sort'];
      public  $timestamps=false;
  
      public function country()
      {
          return $this-&gt;belongsTo('App\Models\Country');
      }
  
      public function city()
      {
          return $this-&gt;hasMany('App\Models\City');
      }
  }
  </code></pre>
  
  <h2>City</h2>
  
  <pre><code>class City extends Model
  {
      protected $table='cities';
      protected $fillable=['region_id','name','sort'];
      public  $timestamps=false;
  
      public function region()
      {
          return $this-&gt;belongsTo('App\Models\Region');
      }
  }
  </code></pre>
  
  <p>When we remove the country automatically, remove all child item relationship, that is, removed and regions and city this country</p>
  
  <p>I am doing so: </p>
  
  <h2>Model Country</h2>
  
  <pre><code>    public  static function boot() {
          parent::boot();
  
          static::deleting(function($country) {
              //remove related rows region and city
  
              // need an alternative variation of this code
              $country-&gt;region()-&gt;city()-&gt;delete();//not working
              $country-&gt;region()-&gt;delete();
  
              return true;
          });
      }
  }
  </code></pre>
  
  <h2>OR</h2>
  
  <h2>Model Region</h2>
  
  <pre><code>public  static function boot() {
          parent::boot();
          // this event do not working, when delete a parent(country)
          static::deleting(function($region) {
              dd($region);
              //remove related rows city
              $region-&gt;city()-&gt;delete();
              return true;
          });
      }
  }
  </code></pre>
  
  <p><strong>options with cascading deletes database, please do not offer</strong></p>
  
  <h1>UPDATE</h1>
  
  <p><strong>I found the answer</strong></p>
  
  <p>use closure for query builder, to remove related models</p>
  
  <h2>Model Country</h2>
  
  <pre><code>public  static function boot() {
          parent::boot();
  
          static::deleting(function($country) {
              //remove related rows region and city
              $country-&gt;region-&gt;each(function($region) {
                  $region-&gt;city()-&gt;delete();
              });
              $country-&gt;region()-&gt;delete();//
              return true;
          });
      }
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/23232912/laravel-eloquent-orm-removing-rows-and-all-the-inner-relationships">Laravel Eloquent ORM - Removing rows and all the inner relationships</a></p>