title: How use conditional relationship in eloquent laravel
body: <p>I have a 'conversation_message' table and separate sender role by column 'sender_type' (admin/user). Both of admin &amp; user in a different table. But when I call the model, that showed error <em>Call to a member function addEagerConstraints() on null</em></p>
  
  <p><br>
  Table column and data</p>
  
  <pre><code>| id | id_group | id_reply | id_sender | sender_type | message 
  | 1  | 1 | null | 3 | admin | Hi, I'm admin
  | 2  | 1 | 1 | 3 | admin | I wanna give u promo
  | 3  | 1 | 2 | 18 | user | What's promo ?
  </code></pre>
  
  <p>I've tried if conditional with column value, but it doesn't work.</p>
  
  <p><strong>Conversation_message.php</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>public function sender(){
          switch($this-&gt;sender_type){
              case "user":
                  return $this-&gt;belongsTo(User::class, 'id_sender', 'id');
              case "admin":
                  return $this-&gt;belongsTo(Admin::class, 'id_sender', 'id');
              default:
                  return;           
          }
      }
  </code></pre>
  
  <p><strong>InboxController.php</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>public function message_detail_chat($groupId){
          $data = [];
          $data['messages'] = Conversation_message::with('replies')
                          -&gt;with('sender')
                          -&gt;with('recipients')
                          -&gt;where(['id_group' =&gt; $groupId])-&gt;get();
  }
  </code></pre>
  
  <p>I expect to use conditional model by column value 'sender_type' but the actual output is wrong.</p>