title: Laravel relationship between 3 models (group,member,post)
body: <p>I have 3 models that are related to each other. </p>
  
  <ol>
  <li>Group</li>
  <li>GroupMember</li>
  <li>GroupPost</li>
  <li>GroupPostComment</li>
  </ol>
  
  <p>Now, I have separeted the database tables aswell. </p>
  
  <ol>
  <li>groups</li>
  <li>group_members</li>
  <li>group_posts</li>
  <li>group_post_comments</li>
  </ol>
  
  <p>For each group a member is a member of, I insert a new row in group_members.
  The foreign key here is "group_id", wich relates to the id field in groups.
  In group_posts there is a new row for every post posted. This container a group_id field aswell.</p>
  
  <p>What I'm struggling with is the GroupMember model.
  How can I get an collection of all the posts from this GroupMember, that the GroupMember posted in that one group its related to?</p>
  
  <p>Here is my models setup.</p>
  
  <p><strong>Group model</strong></p>
  
  <pre><code>public function category() {
      return $this -&gt; belongsTo('App\GroupCategory', 'category_id');
  }
  
  public function owner() {
      return $this -&gt; hasOne('App\GroupMember', 'group_id') -&gt; where('owner', true);
  }
  
  public function mods() {
      return $this -&gt; hasMany('App\GroupMember', 'group_id') -&gt; where('mod', true);
  }
  
  public function admins() {
      return $this -&gt; hasMany('App\GroupMember', 'group_id') -&gt; where('admin', true);
  }
  
  public function members() {
      return $this -&gt; hasMany('App\GroupMember', 'group_id');
  }
  
  public function posts() {
      return $this -&gt; hasMany('App\GroupPost', 'group_id');
  }
  
  public function comments() {
      return $this -&gt; hasMany('App\GroupPostComment', 'group_id');
  }
  </code></pre>
  
  <p><strong>GroupMember model</strong></p>
  
  <pre><code>public function group() {
      return $this -&gt; belongsTo('App\Group');
  }
  
  public function posts() {
      return $this -&gt; hasMany('App\GroupPost', 'group_id', 'group_id');
  }
  
  public function comments() {
      return $this -&gt; hasMany('App\GroupPostComment', 'group_id', 'group_id');
  }
  
  public function user() {
      return $this -&gt; belongsTo('App\User');
  }
  </code></pre>
  
  <p><strong>GroupPost model</strong></p>
  
  <pre><code>public function user() {
      return $this -&gt; belongsTo('App\User');
  }
  
  public function comments() {
      return $this -&gt; hasMany('App\GroupPostComment', 'post_id');
  }
  
  public function group() {
      return $this -&gt; belongsTo('App\Group');
  }
  </code></pre>
  
  <p>The problem is the GroupMember model and the posts() / comments() method.
  There is an error in my relation, but I can't see what I am doing wrong.</p>
  
  <p>Does anyone see what I'm doing wrong?</p>