title: Call to undefined method App\Product::whereHas() in laravel
body: <p>Hi try to get my products list in subcategory page but i'm getting this error</p>
  <blockquote>
  <p>Call to undefined method App\Product::whereHas()</p>
  </blockquote>
  <p>here is my function</p>
  <pre><code>// Subategory page included posts
      public function productsubcategory($slug)
      {
         $products = Product::whereHas('subcategory.category', function($q) use($slug){
         $q-&gt;where('slug',$slug);
         })-&gt;paginate(6);
          return view('frontend.subcategories', compact('products'));
      }
  </code></pre>
  <p>this is my route:</p>
  <pre><code>Route::get('/category/{slug}/subcategory/{subslug}', ['as' =&gt; 'showbysub', 'uses' =&gt; 'IndexController@productsubcategory']);
  </code></pre>
  <p>my product model:</p>
  <pre><code>public function categories(){
       return $this-&gt;belongsTo(Category::class);
    }
  
    public function subcategories(){
       return $this-&gt;belongsTo(Subcategory::class);
    }
  </code></pre>
  <p>category model:</p>
  <pre><code>public function products(){
       return $this-&gt;hasMany(Product::class);
    }
  
    public function subcategories(){
       return $this-&gt;hasMany(Subcategory::class);
    }
  </code></pre>
  <p>subcategory model:</p>
  <pre><code>public function category(){
       return $this-&gt;belongsTo(Category::class, 'category_id');
    }
  
    public function products(){
       return $this-&gt;hasMany(Product::class);
    }
  </code></pre>
  <p>any idea?</p>
  <p><strong>UPDATE:</strong></p>
  <p>I changed my function to:</p>
  <pre><code>public function productsubcategory($slug)
      {
          $subcategories = Subcategory::where('slug','=',$slug)-&gt;with('products')-&gt;paginate(6);
          return view('frontend.subcategories', compact('subcategories'));
      }
  </code></pre>
  <p>and now my page is loading but will not show any item (product).</p>
  <h1>UPDATE 2</h1>
  <p>I changed my function to this:</p>
  <pre><code>public function productsubcategory($slug)
      {
          $products = Product::with('subcategories')
          -&gt;orderBy('id', 'desc')
          -&gt;get();
          return view('frontend.subcategories', compact('products'));
      }
  </code></pre>
  <blockquote>
  <p>Now I can get products but the problem is all products will show even
  if they're included other categories.</p>
  </blockquote>
  <p>How to solve that?</p>