Laravel 4.2 n+1 without eager loading
<p>I have a table with products</p>

<pre><code>products
------------------------------------------------
id  product_code    purchase_type   quantity
1   106             new             1
2   107             renew           3
</code></pre>

<p>and a pricelist for products</p>

<pre><code>price_list
----------------------------------------------------------------------
id  product_code    purchase_type   minimum     maximum     unit_price
1   106             new             1           25          20
2   106             new             26          50          16
3   106             new             51          100         14
</code></pre>

<p>When I display the products on a page, I need to display the price with the formula</p>

<pre><code>getPrice(product_code, purchase_type, quantity) for every product
</code></pre>

<p>Using</p>

<pre><code>foreach(Product::all() as $product){
    {{ $product-&gt;product_code }}
    {{ $product-&gt;purchase_type }}
    {{ $product-&gt;quantity }}
    {{ PriceList::getPrice($product-&gt;product_code, $product-&gt;purchase_type, $product-&gt;quantity) }} - this one is the n+1 problem
}
</code></pre>

<p><strong>PriceList::getPrice()</strong> is something like: </p>

<pre><code>public static function getPrice($productCode, $purchaseType, $quantity){
    return PriceList::where('product_code', $productCode)
        -&gt;where('purchase_type', $purchaseType)
        -&gt;where('minimum', '&gt;=', $quantity)
        -&gt;where('maximum', '&lt;=', $quantity)
        -&gt;get()-&gt;first()-&gt;unit_price;
}
</code></pre>

<p>I can't seem to find a more efficient way.
And when I display over 1000 products, then it gets really slow.
I can't seem to find a way to use eager loading.</p>