title: Laravel/Eloquent relation using id value combination of 2 columns
body: <p>The simplified info: I have a database where I have a table with columns 'code1' and 'code2', these are used to make an id for another table where the id column is 'code1.code2'. The columns are varchar columns.</p>
  
  <p>Full info: If you are wondering I am working on an importer for geonames database and the geonames table has country_code and admin1_codes and they have another table called admin1_codes and that table has an ID column of country_code.admin1_code (concataneted with dot). If you need further info about geonames database, it is freely available at their web site.</p>
  
  <p>So I want to make a relation using eloquent models but it seems impossible. I am thinking that I need to make my own custom db relation class for this.</p>
  
  <p>So the question is, what would be the best way to accomplish this task?</p>
  
  <p>Thanks!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I came up with a workaround using a scope:</p>
  
  <pre><code>$query = $query-&gt;leftJoin('table2',function($join) {
      $join-&gt;on(
          DB::raw('table2.code = CONCAT_WS(\'.\','.
                  'table1.code1,'.
                  'table1.code2)'),
          DB::raw(''),
          DB::raw('')
     );
  });
  return $query;
  </code></pre>
  
  <p>This successfully joins the relevant row while matching the codes. However I would like to setup a relation between tables. Any ideas on if this is even possible using Eloquent? or can you think about a more elegant solution?</p>