title: Laravel Eloquent How to Combine Many to Many and One to Many relationships with 3 table
body: <p>I have a database like this.</p>
  
  <pre><code>+----------------+
  | Tables_in_test |
  +----------------+
  | a_b            |
  | a_c            |
  | as             |
  | bs             |
  | cs             |
  +----------------+
  </code></pre>
  
  <p>bs and cs tables have many to many relationship with as table. So a_b table and a_c table are pivot tables.</p>
  
  <p>This is as table </p>
  
  <pre><code>+----+------+
  | id | name |
  +----+------+
  |  1 | A1   |
  |  2 | A2   |
  +----+------+
  </code></pre>
  
  <p>This is bs table</p>
  
  <pre><code>+----+------+
  | id | name |
  +----+------+
  |  1 | B1   |
  +----+------+
  </code></pre>
  
  <p>And this is cs table </p>
  
  <pre><code>+----+------+------+
  | id | b_id | name |
  +----+------+------+
  |  1 |    1 | C1   |
  |  2 |    1 | C2   |
  +----+------+------+
  </code></pre>
  
  <p>this is a_b pivot table</p>
  
  <pre><code>+------+------+
  | a_id | b_id |
  +------+------+
  |    1 |    1 |
  +------+------+
  </code></pre>
  
  <p>And this is a_c pivot table.</p>
  
  <pre><code>+------+------+
  | a_id | c_id |
  +------+------+
  |    1 |    1 |
  |    2 |    2 |
  +------+------+
  </code></pre>
  
  <p>And this is my A model for the as table. </p>
  
  <pre><code>class A extends Model
  {
      protected $table = "as";
      public function b(){
          return $this-&gt;belongsToMany("App\B");
      }
      public function c(){
          return $this-&gt;belongsToMany("App\C");
      }
  }
  </code></pre>
  
  <p>And this is B model for bs table</p>
  
  <pre><code>class B extends Model
  {
      protected $table = "bs";
      public function c(){
          return $this-&gt;hasMany("App\C");
      }
  }
  </code></pre>
  
  <p>I just want to query C table values as related to A table.</p>
  
  <p>I tried this query </p>
  
  <pre><code>A::where("id",1)-&gt;with("b.c")-&gt;get();
  </code></pre>
  
  <p>But this result also gives me C2 value which is related to A2 in "<strong>as</strong>" table. I want to get only C1 value which is related only A1 value in "<strong>as</strong>" table. </p>
  
  <p>How can I do this ? Thank you for your help</p>