title: Laravel - Join with a table with composite primary key
body: <p>My problem is to join 2 tables in Laravel framework. One is <strong>dynamic name table</strong> (it's a variable) and second has <strong>composite primary key</strong>. I have to use query builder instead of where(). Please view my following for details:</p>
  
  <p>I have 2 tables:</p>
  
  <pre><code>CREATE TABLE `details` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `source_id` int(10) unsigned NOT NULL,
    `brand_id` int(10) DEFAULT NULL,
    PRIMARY KEY (`id`)
  );
  CREATE TABLE `links` (
    `source_id` int(10) unsigned NOT NULL,
    `brand_id` tinyint(3) unsigned NOT NULL DEFAULT '1',
    PRIMARY KEY (`source_id`,`brand_id`)
  );
  </code></pre>
  
  <p>Now, I need to join 2 these tables, I use this code:</p>
  
  <pre><code>&lt;?php $results =  \DB::table('details')
              -&gt;join('links', function($join)
              {
                  $join-&gt;on('details.source_id', '=',  'links.source_id');
                  $join-&gt;on('details.brand_id','=', 'links.brand_id');
              })
              -&gt;get();?&gt;
  </code></pre>
  
  <p>It's quite simple to join these table, OK. But my problem is the table name is dynamic.</p>
  
  <pre><code>&lt;?php 
  $type = Input::get('type', null);
  $table = $type . '_details';
  $results =  \DB::table($table)
                  -&gt;join('links', function($join)
                  {
                      // the following code will show errors undefined $table
                      $join-&gt;on($table . '.source_id', '=',  'links.source_id');
                      $join-&gt;on($table . '.brand_id','=', 'links.brand_id');
                  })
                  -&gt;get();
  
  ?&gt;
  </code></pre>
  
  <p>Please help me to solve this problem.
  Many thanks!!!</p>