title: BadMethodCallException: Call to undefined method Illuminate\Database\Query\Builder::detach()
body: <p>I'm new to Laravel.</p>
  
  <p>This is what I'm trying to do.</p>
  
  <p>When building record is deleted, the related room record is deleted as well. In addition to that, when the related room record is deleted on building record being deleted, I want to set null to the foreign key to room id on the related visit_records record.</p>
  
  <p>When I try to detach the relationship between room model record and visitingRecord model record, it causes an error.</p>
  
  <p>How can I make it work??</p>
  
  <blockquote>
    <p>Building => Room => VisitingRecord</p>
    
    <p>Building has many rooms. Room belongs to a building.  Room has many
    visit records.  Visit Record belongs to a room.</p>
  </blockquote>
  
  <h1>Building Model Class</h1>
  
  <p>
  
  <pre><code>namespace App\Model;
  
  use Illuminate\Database\Eloquent\Model;
  use Illuminate\Database\Eloquent\SoftDeletes;
  
  
  class Building extends Model
  {
  
      use SoftDeletes;
      protected $dates = ['deleted_at'];
  
      public $table = "buildings";
  
  
  
      public function rooms()
      {
  
          return $this-&gt;hasMany("App\Model\Room");
  
      }
  
  
      public static function boot(){
  
          parent::boot();
  
          static::deleted(function($building)
          {
  
              //This 4 lines cause an error.
  
              $building-&gt;rooms()-&gt;each(function($room) {
                  $room-&gt;visitRecords()-&gt;detach();
                  $room-&gt;save();
              });
  
              //this line is ok.
              $building-&gt;rooms()-&gt;delete();
  
  
          });
  
      }
  
  
  
  
  
  
  }
  </code></pre>
  
  <h1>log</h1>
  
  <pre><code>[2017-05-15 13:36:02] local.ERROR: BadMethodCallException: Call to undefined method Illuminate\Database\Query\Builder::detach() in /vagrant/door/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2451
  Stack trace:
  #0 /vagrant/door/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(1250): Illuminate\Database\Query\Builder-&gt;__call('detach', Array)
  #1 [internal function]: Illuminate\Database\Eloquent\Builder-&gt;__call('detach', Array)
  #2 /vagrant/door/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Relation.php(340): call_user_func_array(Array, Array)
  #3 /vagrant/door/app/Model/Building.php(62): Illuminate\Database\Eloquent\Relations\Relation-&gt;__call('detach', Array)
  #4 /vagrant/door/vendor/laravel/framework/src/Illuminate/Database/Concerns/BuildsQueries.php(56): App\Model\Building::App\Model\{closure}(Object(App\Model\Room), 0)
  #5 /vagrant/door/vendor/laravel/framework/src/Illuminate/Database/Concerns/BuildsQueries.php(35): Illuminate\Database\Eloquent\Builder-&gt;Illuminate\Database\Concerns\{closure}(Object(Illuminate\Database\Eloquent\Collection))
  #6 /vagrant/door/vendor/laravel/framework/src/Illuminate/Database/Concerns/BuildsQueries.php(60): Illuminate\Database\Eloquent\Builder-&gt;chunk(1000, Object(Closure))
  #7 [internal function]: Illuminate\Database\Eloquent\Builder-&gt;each(Object(Closure))
  #8 /vagrant/door/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Relation.php(340): call_user_func_array(Array, Array)
  #9 /vagrant/door/app/Model/Building.php(64): Illuminate\Database\Eloquent\Relations\Relation-&gt;__call('each', Array)
  #10 /vagrant/door/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(348): App\Model\Building::App\Model\{closure}(Object(App\Model\Building))
  #11 /vagrant/door/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(199): Illuminate\Events\Dispatcher-&gt;Illuminate\Events\{closure}('eloquent.delete...', Array)
  #12 /vagrant/door/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(172): Illuminate\Events\Dispatcher-&gt;dispatch('eloquent.delete...', Array, false)
  #13 /vagrant/door/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php(148): Illuminate\Events\Dispatcher-&gt;fire('eloquent.delete...', Object(App\Model\Building))
  #14 /vagrant/door/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(754): Illuminate\Database\Eloquent\Model-&gt;fireModelEvent('deleted', false)
  #15 /vagrant/door/app/Http/Controllers/BuildingController.php(274): Illuminate\Database\Eloquent\Model-&gt;delete()
  #16 [internal function]: App\Http\Controllers\BuildingController-&gt;destroy('2', Object(App\Http\Requests\BuildingRequest))
  #17 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(55): call_user_func_array(Array, Array)
  #18 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(44): Illuminate\Routing\Controller-&gt;callAction('destroy', Array)
  #19 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Route.php(203): Illuminate\Routing\ControllerDispatcher-&gt;dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\BuildingController), 'destroy')
  #20 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Route.php(160): Illuminate\Routing\Route-&gt;runController()
  #21 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Router.php(559): Illuminate\Routing\Route-&gt;run()
  #22 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Routing\Router-&gt;Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  #23 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  #24 /vagrant/door/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Routing\Middleware\SubstituteBindings-&gt;handle(Object(Illuminate\Http\Request), Object(Closure))
  #25 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
  #26 /vagrant/door/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(65): Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  #27 /vagrant/door/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken-&gt;handle(Object(Illuminate\Http\Request), Object(Closure))
  #28 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
  #29 /vagrant/door/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  #30 /vagrant/door/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\View\Middleware\ShareErrorsFromSession-&gt;handle(Object(Illuminate\Http\Request), Object(Closure))
  #31 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
  #32 /vagrant/door/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(64): Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  #33 /vagrant/door/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Session\Middleware\StartSession-&gt;handle(Object(Illuminate\Http\Request), Object(Closure))
  #34 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
  #35 /vagrant/door/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  #36 /vagrant/door/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse-&gt;handle(Object(Illuminate\Http\Request), Object(Closure))
  #37 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
  #38 /vagrant/door/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(59): Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  #39 /vagrant/door/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Cookie\Middleware\EncryptCookies-&gt;handle(Object(Illuminate\Http\Request), Object(Closure))
  #40 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
  #41 /vagrant/door/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  #42 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Router.php(561): Illuminate\Pipeline\Pipeline-&gt;then(Object(Closure))
  #43 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Router.php(520): Illuminate\Routing\Router-&gt;runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
  #44 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Router.php(498): Illuminate\Routing\Router-&gt;dispatchToRoute(Object(Illuminate\Http\Request))
  #45 /vagrant/door/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(174): Illuminate\Routing\Router-&gt;dispatch(Object(Illuminate\Http\Request))
  #46 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Foundation\Http\Kernel-&gt;Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
  #47 /vagrant/door/vendor/barryvdh/laravel-debugbar/src/Middleware/Debugbar.php(51): Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  #48 /vagrant/door/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Barryvdh\Debugbar\Middleware\Debugbar-&gt;handle(Object(Illuminate\Http\Request), Object(Closure))
  #49 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
  #50 /vagrant/door/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  #51 /vagrant/door/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest-&gt;handle(Object(Illuminate\Http\Request), Object(Closure))
  #52 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
  #53 /vagrant/door/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  #54 /vagrant/door/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest-&gt;handle(Object(Illuminate\Http\Request), Object(Closure))
  #55 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
  #56 /vagrant/door/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  #57 /vagrant/door/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\ValidatePostSize-&gt;handle(Object(Illuminate\Http\Request), Object(Closure))
  #58 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
  #59 /vagrant/door/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  #60 /vagrant/door/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode-&gt;handle(Object(Illuminate\Http\Request), Object(Closure))
  #61 /vagrant/door/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
  #62 /vagrant/door/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  #63 /vagrant/door/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(149): Illuminate\Pipeline\Pipeline-&gt;then(Object(Closure))
  #64 /vagrant/door/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel-&gt;sendRequestThroughRouter(Object(Illuminate\Http\Request))
  #65 /vagrant/door/public/index.php(53): Illuminate\Foundation\Http\Kernel-&gt;handle(Object(Illuminate\Http\Request))
  </code></pre>