title: Laravel Eloquent query recursive relationship model with pagination
body: <p>I am building a store, where I have to display to the user all products in a given category and all other products that are contained in the subsequent subcategories of the currently accessed one. The categories have the N+1 problem since there can be infinite subcategories. I want to be able to filter trough these products and also to be able to paginate them.
  This is my categories model:</p>
  <pre><code>   class CatalogCategory extends Model
     {
  
      public function parent()
      {
          return $this-&gt;belongsTo('App/CatalogCategory','parent_id');
      }
  
      public function children()
      {
          return $this-&gt;hasMany($this,'parent_id')
              -&gt;orderBy('order_place','ASC')
              -&gt;with('children');
      }
  
      /*
      *   Return products, that belong just to the parent category.
      */
  
      public function products()
      {
          return $this-&gt;hasMany('App\CatalogProduct','parent_id')
              -&gt;where('is_active', 1)
              -&gt;whereDate('active_from', '&lt;=', Carbon::now('Europe/Sofia'))
              -&gt;orderBy('created_at','DESC');
      }
  
      /*
      *   Return all products contained in the parent category and its children categories.
      */
  
      public function all_products()
      {
          $products = $this-&gt;products;
  
          foreach ($this-&gt;children as $child) {
              $products = $products-&gt;merge($child-&gt;all_products());
          }
  
          return $products;
      }
  
  }
  </code></pre>
  <p>The all_products() method returns all of the products, that I want, but since it's a collection i'm unable to paginate or filter through it. My question is if there is a better way to retrieve the products and how to retrieve them so, that i can query them for filtering and paginate them?</p>