title: Laravel condition (depending another table) in relation belongsTo for 3 tables
body: <p>I have a problem using eloquent model with Laravel 5 and getting some conditions to relation.</p>
  
  <p>My db have 3 tables</p>
  
  <p><strong>Voivodeships</strong> structure with example data</p>
  
  <pre><code>[id] [name]
  1    voiv1
  2    voiv2
  3    voiv3
  </code></pre>
  
  <p><strong>counties</strong> structure with example data</p>
  
  <pre><code>[id] [name] [voivode]
  1    count1 1
  2    count2 1
  1    count3 2
  2    count4 2
  </code></pre>
  
  <p><strong>community</strong> structure with example data</p>
  
  <pre><code>[id] [name] [voivode] [county]
  1    comm1  1         1
  2    comm2  1         1
  1    comm3  2         1
  2    comm4  2         1
  </code></pre>
  
  <p><strong>city</strong> structure with example data</p>
  
  <pre><code>[id] [name] [community] [county] [voivode]
  1    city1  1           1        2
  2    city2  2           1        2
  3    city3  2           1        1
  </code></pre>
  
  <p>Now I'm getting city by its ID and also need to get name of voivodeship, county and community, there will be no problem but need to add condition in relation because of same community id can be assigned to many voivodes.</p>
  
  <p>So my raw query will be</p>
  
  <pre><code>DB::raw("SELECT
      c.name, co.name as comName, cou.name as couName, v.name as voivName
      FROM city c
      LEFT JOIN community co ON c.community=co.id
      LEFT JOIN counties cou ON c.county=cou.id
      LEFT JOIN voivodeships v ON c.voivode=v.id
      WHERE c.id=3
      AND
      (co.voivode=c.voivode AND co.county=c.county)
      AND
      (cou.id=c.county AND cou.voivode=c.voivode)
      ")
  </code></pre>
  
  <p>All works ok with relation but because of not </p>
  
  <pre><code>AND
  (co.voivode=c.voivode AND co.county=c.county)
  AND
  (cou.id=c.county AND cou.voivode=c.voivode)
  ")
  </code></pre>
  
  <p>in relation db is returning correct cityname and voiv name but countyname and community name is first matching id what is correct but not data I need.</p>
  
  <p>I try to add to my relation method ->where like this</p>
  
  <pre><code>public function community()
  {
      return
      $this-&gt;belongsTo('App\Models\Communities', 'community', 'id')
      -&gt;where('voiv', '=', 'city.voiv')
      -&gt;where('county', '=', 'city.county')
      -&gt;select(['id', 'name']);
  }
  </code></pre>
  
  <p>But doesn't work...</p>
  
  <p><strong>edit (answer for comments)</strong></p>
  
  <p>Yes, I'm looking to display all, cityname, communityname, countyname and voivodeshipname.</p>
  
  <p>County and community id is unique but with voivode,county for communities an with voivode for counties.
  So e.g for communities for given voivode and county there are no 2 communities with same id.
  And for counties for given voivode there are no 2 counties with same id.</p>
  
  <p>Method community is in cities model. 
  explaining "in relation db is returning correct cityname and voiv name but countyname and community name is first matching id what is correct but not data I need" mean that in my case db is getting e.g first matching community with provided id (what is wrong because it's unique as I wrote above and that is the reason I need condition in relation).</p>
  
  <p>So in other words I want to achieve the same result as from provided raw query with eloquent model and relations. Just need to add to relation where statement with condition depending on main table in this example "city".</p>