title: How to bind parameter in sub query in Laravel?
body: <p>I have a sub-query in join in which I am using where clause, but its giving me the error</p>
  <blockquote>
  <p>Column not found: 1054 Unknown column 'products.id' in 'where clause'</p>
  </blockquote>
  <p>Below is my query:</p>
  <pre><code>Product::leftJoin(
                  'categories',
                  'products.category_id',
                  '=',
                  'categories.id'
              )
              -&gt;join('vendor_products', function ($join) {
                  $join-&gt;on(
                      'products.id',
                      '=',
                      'vendor_products.product_id'
                  )
              })
              -&gt;leftJoin('brand_products', function ($join) {
                      $join-&gt;on(
                          'products.id',
                          '=',
                          'brand_products.product_id'
                      )-&gt;on(
                          'brand_products.brand_id',
                          '=',
                          DB::raw('(
                              SELECT brand_id FROM 
                              (SELECT brand_id, COUNT(brand_id) productCount 
                              FROM brand_products
                              where brand_products.product_id = products.id
                              inner join brand on brand_products.brand_id = brand.id
                              GROUP BY brand_id order by productCount desc, DATE(brand.created_at) asc limit 1) as results)')
                      );
              })
              -&gt;get();
  </code></pre>
  <p>this is the line causing error <code>where brand_products.product_id = products.id</code>.</p>
  <p>Any help would be highly appreciable.</p>