title: Eloquent relationship in custom pivot table
body: <p>I might be overcomplicating things, but here's relationship I'm trying to write in Eloquent (Laravel 5):</p>
  
  <p>I have a list of products and price lists. Each product can have many price lists. This is easy to do, but here's another thing - for every product &lt;> price list assignment I can have many prices based on quantity, so it's like:</p>
  
  <pre><code>productID: 1010
  priceListId: 1
  
  min quantity: 1 -&gt; price 10.00
  min quantity: 5 -&gt; price 9.50
  min quantity: 10 -&gt; price 9.00
  
  productID: 1010
  priceListId: 2
  
  min quantity: 1 -&gt; price 15.00
  min quantity: 40 -&gt; price 14.00
  min quantity: 90 -&gt; price 12.00
  </code></pre>
  
  <p>I think I know how to create custom pivot table although I don't know how to use it. I followed <a href="https://github.com/laravel/framework/issues/2093#issuecomment-39154456" rel="nofollow">this link</a> now I'm not sure if my code is correct and how to use it.</p>
  
  <p>At the moment I have:</p>
  
  <p>Product model:</p>
  
  <pre class="lang-php prettyprint-override"><code>class Product extends Model {
  
      public function pricelists()
      {
          return $this-&gt;belongsToMany('PriceList');
      }
  
      public function newPivot(Model $parent, array $attributes, $table, $exists)
      {
          if ($parent instanceof PriceList)
          {
              return new ProductPricePivot($parent, $attributes, $table, $exists);
          }
  
          return parent::newPivot($parent, $attributes, $table, $exists);
      }
  
  }
  </code></pre>
  
  <p>PriceList model:</p>
  
  <pre class="lang-php prettyprint-override"><code>class PriceList extends Model {
  
      public function products()
      {
          return $this-&gt;belongsToMany('Product');
      }
  
      public function newPivot(Model $parent, array $attributes, $table, $exists)
      {
          if ($parent instanceof Product)
          {
              return new ProductPricePivot($parent, $attributes, $table, $exists);
          }
  
          return parent::newPivot($parent, $attributes, $table, $exists);
      }
  }
  </code></pre>
  
  <p>Pivot:</p>
  
  <pre class="lang-php prettyprint-override"><code>class ProductPricePivot extends Pivot {
  
      public function product()
      {
          return $this-&gt;belongsTo('Product');
      }
  
      public function pricelist()
      {
          return $this-&gt;belongsTo('PriceList');
      }
  
      public function prices()
      {
          return $this-&gt;hasMany('ProductPrice');
      }
  
  }
  </code></pre>
  
  <p>Now ProductPrice extends Model again and is just a standard model without any additional methods.</p>
  
  <p>Is this correct? If so, then following example from above, how can I add new quantity/price level to price list 1 on product 1010?</p>
  
  <p>At the moment, when creating relationships, I'm doing:</p>
  
  <pre class="lang-php prettyprint-override"><code>$productID = 1010;
  $priceListID = 1;
  
  $priceList = PriceList::find($priceListID);
  $priceList-&gt;products()-&gt;attach($productID);
  
  $productToPriceList = $priceList-&gt;products()-&gt;find($productID);
  </code></pre>
  
  <p>And I'm lost here... I find this relationship, but how can I now attach next quantity &lt;> price level to it?</p>
  
  <p>Can someone please give an example of how to use such relationship or links to pages where I can find something more about it? And yes, I check Laravel documentation, and yes, I googled it as well.</p>
  
  <p>Thank you!</p>