title: Complex Laravel Relationships
body: <p>I am facing some problems on how to set up my tables/relationships in laravel.</p>
  
  <p><strong>Description:</strong>
  A user can have 3 types (tenant, manager, admin). The admin is the easiest type, it has no extra properties. Tenants and Managers have extra properties pertaining to each type. Furthermore, a tenant has a 1-1 relationship to a unit (assuming for now 1 user account per unit) and a manager has a N-N relationship with the properties (multiple managers can manage 1 property and 1 manager can manage multiple properties).</p>
  
  <p>What I came up with so far:</p>
  
  <h1>Method 1</h1>
  
  <p>User has a polymorphic relationship and belongs to both the tenant and manager.
  <a href="https://i.stack.imgur.com/J2yN1.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/J2yN1.jpg" alt="method1.jpg"></a></p>
  
  <p>How my model relationships would be set up:</p>
  
  <p><code>User.php</code></p>
  
  <pre><code>public function subaccount()
  {
      return $this-&gt;morphTo('subaccount');
  }
  </code></pre>
  
  <p><code>Manager.php</code> and <code>Tenant.php</code></p>
  
  <pre><code>public function user()
  {
      return $this-&gt;morphOne(User::class, 'subaccount');
  }
  </code></pre>
  
  <p><strong>Pros:</strong></p>
  
  <ul>
  <li>No hacky code.</li>
  </ul>
  
  <p><strong>Cons:</strong></p>
  
  <ul>
  <li>Cant set up foreign key constraints, subaccount_id can be null if
  user is an admin</li>
  <li>I feel like users shouldn't belong to tenants/managers, this
  relationship should be the other way</li>
  </ul>
  
  <h1>Method 2</h1>
  
  <p>Both tenant and manager would belong to the user
  <a href="https://i.stack.imgur.com/zOmAC.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zOmAC.jpg" alt="method2.jpg"></a></p>
  
  <p>How my model relationships would be set up:</p>
  
  <p><code>Manager.php</code> and <code>Tenant.php</code> these are fine</p>
  
  <pre><code>public function user()
  {
      return $this-&gt;belongsTo(User::class);
  }
  </code></pre>
  
  <p><code>User.php</code> would need something like this</p>
  
  <pre><code>public function subaccount()
  {
      if ($this-&gt;type === 'tenant') {
          $resource = Tenant::class;
      } else if ($this-&gt;type === 'manager') {
          $resource = Manager::class;
      }
  
      return $this-&gt;hasOne($resource);
  }
  </code></pre>
  
  <p><strong>Pros:</strong></p>
  
  <ul>
  <li>Can set up foreign key constraints, no subaccount_id</li>
  </ul>
  
  <p><strong>Cons:</strong></p>
  
  <ul>
  <li>I feel like that "dynamic" relationship in the user is weird and
  feels hacky.</li>
  </ul>
  
  <p>Would love to get different perspectives on this or thoughts on any other way I can build this.</p>