title: Eager load relationships in laravel with conditions on the relation
body: <p>I have categories related to each other in a tree. Each category <code>hasMany</code> children. Each end category <code>hasMany</code> products. </p>
  
  <p>The products also <code>belongsToMany</code> different types. </p>
  
  <p>I want to eager load the categories with their children and with the products but I also want to put a condition that the products are of a certain type.</p>
  
  <p>This is how my categories Model looks like</p>
  
  <pre><code>public function children()
  {
      return $this-&gt;hasMany('Category', 'parent_id', 'id');
  }
  
  
  public function products()
  {
      return $this-&gt;hasMany('Product', 'category_id', 'id');
  }
  </code></pre>
  
  <p>The Product Model</p>
  
  <pre><code>public function types()
  {
      return $this-&gt;belongsToMany(type::class, 'product_type');
  }
  </code></pre>
  
  <p>In my database I have four tables:
  category, product, type, and product_type</p>
  
  <p>I've tried eager loading like so but it loads all the products and not just the ones that fulfil the condition:</p>
  
  <pre><code>$parentLineCategories = ProductCategory::with('children')-&gt;with(['products'=&gt; function ($query) {
          $query-&gt;join('product_type', 'product_type.product_id', '=', 'product.id')
              -&gt;where('product_type.type_id', '=', $SpecificID);
      }]])-&gt;get();
  </code></pre>