 <p>Try this</p> <pre><code> $criteria = array( "prices" =&gt; array( $request['min'] ? $request['min'] : false, $request['max'] ? $request['max'] : false ), "colors" =&gt; $request['color'] ? $request['color'] : false, "sizes" =&gt; $request['size'] ? $request['size'] : false ); $products = Product::with(['sizes' =&gt; function ($query) use ($criteria) { if($criteria['sizes']) { foreach ($criteria['sizes'] as $size) { $query-&gt;where('size', '=', $size); } } }])-&gt;where(function ($query) use ($criteria) { if($criteria['colors']) { foreach ($criteria['colors'] as $color) { $query-&gt;where('color_id', '=', $color); } } if( $criteria['prices'][0] &amp;&amp; $criteria['prices'][1] ) { $query-&gt;whereBetween('price', $criteria['prices'] ); } })-&gt;get(); </code></pre>