title: How can I define the inverse polymorphic many-to-may relationship in eloquent?
body: <p>I'm new to Laravel here. Some confusion regarding eloquent relationship.</p>
  
  <p>I'm trying to achieve this : <a href="https://i.stack.imgur.com/qkxf4.jpg" rel="nofollow noreferrer">Relationship</a></p>
  
  <p>Here's the <a href="https://i.stack.imgur.com/BLbas.jpg" rel="nofollow noreferrer">ER-Diagram</a></p>
  
  <ul>
  <li>1 Question can be any type (multiple-choices/ordering/matching/etc.)</li>
  <li>1 choice AKA 1 row in each {type} table can be mapped to only 1 question</li>
  </ul>
  
  <p>In the ER-diagram the red hilighted part were my first thought (Polymorphic 1-to-Many) 
  but now I changed it to many-to-may with pivot table instead:</p>
  
  <p>[pivot table]</p>
  
  <p><em>question_id</em> | <em>choiceable_id</em> | <em>choiceable_type</em></p>
  
  <p>However, the question is: <strong>How do I define this type of relationship in the models ?</strong></p>
  
  <ul>
  <li><strong>Is Polymorphic Many-To-Many the correct type?</strong></li>
  <li><strong>What is the correct way to get to the choices given a random question?</strong></li>
  </ul>
  
  <p>Because if I understand the doc. correctly, I need to call each type and specify the table inside, but I feel it's a little messy...</p>
  
  <pre><code>// Question Model
  function get_multiple_choices {
  return $this-&gt;morphedByMany('App\TypeMultipleChoice', 'choiceable')
  
  function get_ordering_choices {
  return $this-&gt;morphedByMany('App\TypeOrderingChoice', 'choiceable')
  
  ...
  </code></pre>
  
  <p>is this code correct ? And I still don't know about the choices for random question part. Somehow I feel like there should be a simpler way to get those choices.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Ok, let me re-phrase it.</p>
  
  <p>Can I use / How to use:</p>
  
  <pre><code>// Question Model
  function choices {
     // $this-&gt;MorphTo,MorphedByMany, etc. ?
  }
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>// Question Model
  function get_multiple_choices {}
  function get_matching_choices {}
  function get_ordering_choices {}
  ... x 4-5 more times
  </code></pre>