title: Query multiple relationships in model october cms
body: <p>Hi I'm trying to make an combined query with October CMS active record implementation to filter out user selected input.</p>
  
  <p>The are four models a <code>Color, Series, Department</code> and <code>Product</code> with the relationships as follows:</p>
  
  <p>The <code>Product</code> model </p>
  
  <pre><code>public $belongsTo = [
    'color' =&gt; 'depcore\parts\Models\Color',
  ];
  
  
  public $belongsToMany = [
    'series' =&gt; [
      'depcore\parts\Models\Series',
      'table' =&gt; 'depcore_parts_products_series',
      'order' =&gt; 'name',
      ],
    'departments' =&gt; [
      'depcore\parts\Models\Department',
      'table' =&gt; 'depcore_parts_products_departments',
      // 'order' =&gt; 'name'
      ]
  ];
  </code></pre>
  
  <p><code>Department</code> and <code>Series</code> model</p>
  
  <pre><code>public $hasMany = [
    'products' =&gt; [
      '\depcore\parts\Models\Product',
      'table' =&gt; 'depcore_parts_products_departments',
    ]
  ];
  </code></pre>
  
  <p>And <code>Color</code> model</p>
  
  <pre><code>public $hasMany = [
    'products' =&gt; [
      '\depcore\Parts\Models\Product'
    ]
  ];
  </code></pre>
  
  <p>The user input is sent via ajax to the function which looks like this right now</p>
  
  <pre><code>public function onFilterProducts(){
  
    $filters = Request::input('Filter');
  
    if ( isset( $filters['colors'] ) or isset( $filters['departments'] ) or isset ( $filters['series'] ) ) {
  
      // $this-&gt;page['products'] = Product::whereIn ( 'color_id', $filters['colors']   )-&gt;take( 10 )-&gt;get();
      $this-&gt;page['products'] = Product::where ( function ( $query ) use  ( $filters ) {
  
        if ( isset ( $filters['colors'] ) ) $query-&gt;whereIn('color_id', $filters['colors']);
        if ( isset ( $filters['series'] ) ) $query-&gt;with(
          ['series'=&gt; function ( $subquery ) {
            $subquery-&gt;whereIn('series_id', $filters['series']);
        }]);
  
      }  )-&gt;take(9)-&gt;get();
  
    }
    else
      $this-&gt;page['products'] =  Product::listFrontEnd();
  
  }
  </code></pre>
  
  <p>As you can see I'm trying to make a filter on the many-to-many relation with the series model after the color query (this one works OK).</p>
  
  <p>The problem is with the many-to-many relation I've tried to use different approaches to this problem with either no error ( but also no result ) or with an error saying that the below function <code>where</code> does not work.</p>
  
  <pre><code>if ( isset ( $filters['series'] ) ) $query-&gt;series()-&gt;whereIn ( 'series', $filters['series'] );
  </code></pre>
  
  <blockquote>
    <p>"Call to undefined method depcore\parts\Models\Product::where()" on
    line 1421 of
    /var/www/public/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php</p>
  </blockquote>
  
  <p>I don't know if this is possible to achieve this way or do I need to make a different approach? </p>