title: Laravel Eloquent Relationship hasMany hasOne
body: <p>I am trying to get my head around Laravel's relationships but having difficulty with the last part of what I'm trying to achieve.</p>
  
  <p>My tables are like so:</p>
  
  <p><strong>pagetemplates</strong></p>
  
  <pre><code>id
  </code></pre>
  
  <p><strong>pages</strong></p>
  
  <pre><code>id
  pagetemplate_id
  </code></pre>
  
  <p><strong>pagetemplate_blocks</strong></p>
  
  <pre><code>id
  pagetemplate_id
  </code></pre>
  
  <p><strong>pagetemplateblocks_content</strong></p>
  
  <pre><code>id
  page_id
  page_template_block_id
  </code></pre>
  
  <p>So when loading a page out of the DB I need to get it with pagetemplates, with it's blocks and with the content.</p>
  
  <p>Here's my code so far:</p>
  
  <p><strong>Page.php</strong></p>
  
  <pre><code>public function pageTemplate()
  {
      return $this-&gt;belongsTo('PageTemplate', 'pagetemplate_id')-&gt;with('blocks');
  }
  
  public function pagetemplateblockcontent()
  {
      return $this-&gt;belongsToMany('PageTemplateBlockContent', 'pagetemplateblocks_content', 'page_id', 'page_template_block_id')-&gt;withTimestamps();
  }
  
  public function pagecontent()
  {
      return $this-&gt;hasMany('PageTemplateBlockContent', 'page_id')-&gt;with('pagetemplateblock');
  }
  </code></pre>
  
  <p><strong>PageTemplate.php</strong></p>
  
  <pre><code>public function page()
  {
      return $this-&gt;hasMany('Page', 'pagetemplate_id');
  }    
  
  public function blocks() {
      return $this-&gt;hasMany('PageTemplateBlock', 'pagetemplate_id')-&gt;orderBy('sort_order', 'asc')-&gt;with('blockcontent');
  }
  </code></pre>
  
  <p><strong>PageTemplateBlock.php</strong></p>
  
  <pre><code>public function pagetemplate() {
      return $this-&gt;belongsTo('PageTemplate', 'pagetemplate_id');
  }
  
  public function blockcontent() {
      return return $this-&gt;hasOne('PageTemplateBlockContent');
  }
  </code></pre>
  
  <p><strong>PageTemplateBlockContent.php</strong></p>
  
  <pre><code>public function pagetemplateblock()
  {
      return $this-&gt;belongsTo('PageTemplateBlock', 'page_template_block_id');
  }
  </code></pre>
  
  <p>However, the issue is with the <code>content</code>, if I try this it returns one instance of PageTemplateBlockContent, which is the same for all of the pages. Although there should be a different PageTemplateBlockContent for each Page. I'm not sure how to get around this, so any ideas would be greatly appreciated.</p>
  
  <p><strong>UPDATED</strong></p>
  
  <p>My Controller calls</p>
  
  <pre><code>$this-&gt;pageRepo-&gt;where('id', $id)-&gt;with('pageTemplate');
  </code></pre>
  
  <p>"pageTemplate" returns the following:</p>
  
  <pre><code>return $this-&gt;belongsTo('PageTemplate', 'pagetemplate_id')-&gt;with('blocks');
  </code></pre>
  
  <p>"blocks" returns the following:</p>
  
  <pre><code>return $this-&gt;hasMany('PageTemplateBlock', 'pagetemplate_id')-&gt;orderBy('sort_order', 'asc')-&gt;with('blockcontent');
  </code></pre>
  
  <p>"blockcontent" returns the following:</p>
  
  <pre><code>return $this-&gt;hasOne('PageTemplateBlockContent');
  </code></pre>
  
  <p>So this means it's never hitting the "pagetemplateblockcontent" which Joost suggested creating.</p>