title: Laravel Eloquent and Multiple Joins
body: <p>I understand how to use Eloquent for basic queries and relationships, but I start getting confused when selecting information based on relationships in multiple tables.</p>
  
  <p>For example, i can get the data I need from the database using the the query builder as follows: </p>
  
  <pre><code>    $data['products'] = DB::table('product')
      -&gt;select('product.id', 'product.ref_num', 'productdetails.name')
      -&gt;join('productdetails', function($join)
      {
          $join-&gt;on('product.id', '=', 'productdetails.product_id')
               -&gt;where('productdetails.website_id', '=', '7');
      })
      -&gt;leftJoin('product_category', function($join) use($submenu_id){
          $join-&gt;on('product.id', '=', 'product_category.product_id')
              -&gt;where('product_category.category_id', '=', $submenu_id);
      })
      -&gt;leftJoin('product_type', function($join) use($type_id){
          $join-&gt;on('product.id', '=', 'product_type.product_id')
              -&gt;where('product_type.type_id', '=', $type_id);
      })
      -&gt;get();
  </code></pre>
  
  <p>Basically, i'm getting data from the product and productdetails tables based on which category the product is part of and what type of product it is; These are defined by inner joins to pivot tables product_type and product_category.</p>
  
  <p>Now assuming i have the eloquent relationships set up correctly, how would i go about doing this in Eloquent?</p>
  
  <p>Here are the relevant parts of the Eloquent Models</p>
  
  <p>Product</p>
  
  <pre><code>class Product extends Eloquent{
  
  public function productdetails()
  {
      return $this-&gt;hasMany('Productdetail');
  
  public function categories()
  {
      return $this-&gt;belongsToMany('Category', 'product_category', 'product_id', 'category_id');
  }
  
  public function types()
  {
      return $this-&gt;belongsToMany('Producttype', 'product_type', 'product_id', 'type_id');
  }
  }
  </code></pre>
  
  <p>Product Details</p>
  
  <pre><code>class Productdetail extends Eloquent
  {
  
  
  public function product()
  {
      return $this-&gt;belongsTo('Product');
  }
  }
  </code></pre>
  
  <p>ProductType</p>
  
  <pre><code>class ProductTypes extends Eloquent{
  
  
  function products()
  {
      return $this-&gt;belongsToMany('products', 'product_id', 'type_id', 'product_id');
  }
  </code></pre>
  
  <p>Category</p>
  
  <pre><code>class Category extends Eloquent{
  
  public function products()
  {
      return $this-&gt;belongsToMany('product', 'product_category', 'category_id', 'product_id');
  }
  }
  </code></pre>
  
  <p>Thanks in advance</p>