title: Laravel Model Observers & Relations
body: <p>I have a Member, Person and Address model. They are connected with One to One relationships. Member has one Person,  Person has one Address, Address belongs to Person, and Person belongs to Member etc. </p>
  
  <p>Now, my Address can also belongTo a Location... Resulting in 2 nullable colums, <code>person_id</code> &amp; <code>location_id</code>  </p>
  
  <p>I have created an observer on the <code>Address model</code>. When I am deleting a Member, I want the address to be kept, if the <code>location_id</code> is set.</p>
  
  <pre><code>public function deleting(Address $address)
  {
      if($address-&gt;location_id != null) {
          $address-&gt;person_id = null;
          $address-&gt;save();
          return false;
      } else {
          return true;
      }
  }
  </code></pre>
  
  <p>Weird thing is when I use ... <code>-&gt;address()-&gt;delete();</code> ... It will remove the address with the person, and the Observerfunction won't fire. I have to delete the address first separately  <code>$address-&gt;delete();</code>, and now the Observerfunction will fire.
  Another problem I was thinking about, what if it is the other way around? Deleting a Location with the <code>'person_id'</code> set? </p>
  
  <p>Is there a better way to handle deleting a model that (can) belongsTo 2 different models? (Only OneToOne relationships). </p>
  
  <p>I was thinking maybe I have to make 2 Observers, for Member and Location, and drop the one on the Address model? Or not use observers at all</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>So the situation can occur that a person and a location can have the same  Address. In this case both 'person_id' &amp; 'location_id' are != null on the Address model. </p>
  
  <p>So far I have all the functions on the MemberController working. In my case
  public function destroy( $id )  will only softdelete a member, and keep the Person and Address untouched. </p>
  
  <p>All I need to fix now is forceDeleting, I've been able to come up with some stuff, but I am not sure what is a good way. Something smells :P
  When I delete a Member like this</p>
  
  <pre><code>$member-&gt;person()-&gt;delete();
  $member-&gt;forceDelete();
  </code></pre>
  
  <p>This would work. But I will have to do 3 deletes? (including the address).
  Also deleting an address should depend on the presence of a location_id, because when an address also belongs to a Location, I would like the address to be kept, and instead set the person_id on null.</p>
  
  <p>I have tried this by making an observer on the address model, but with this Im facing the problem of not knowing wether a Member or a Location is being deleted, and so I am not sure which '_id' should be checked for being null or not. </p>
  
  <p>Another problem I am facing is the observer function wont fire when I am deleting the Member->relation()-> way. It only seems to work when I am deleting the address separately $address->delete(); 
  because of this Im doubting if observer is even a good way, and maybe I should just do it in the Member/Location controller. </p>
  
  <pre><code>/**
   * Permanently remove the specified resource from storage.
   *
   * @param int $id
   * @return Response
   */
  public function remove( $id )
  {
      $member = $this-&gt;model-&gt;onlyTrashed()-&gt;find( $id );
  
      $address = $member-&gt;person-&gt;address;
      if($address-&gt;location_id != null) {
          $address-&gt;person_id == null;
          $address-&gt;save();
      } else {
          $address-&gt;delete();
      }
  
      $member-&gt;person()-&gt;delete();
      $member-&gt;forceDelete();
  </code></pre>
  
  <p>This would work, maybe even a case of keeping it stupid simple? But I am not sure if doing this Address stuff here is a good way, just trying to learn. </p>
  
  <p>Member | hasOne - belongsTo | Person | hasOne - belongsTo | Address | belongsTo - hasOne | Location</p>