title: Laravel whereHas with polymorphic relationship
body: <p>Laravel 5.8. I have a table <code>cats</code>:</p>
  
  <pre><code>id | name
  ---------
  1  | jim
  2  | mary
  </code></pre>
  
  <p>and a table <code>colors</code>:</p>
  
  <pre><code>id | color
  ----------
  1  | white
  2  | black
  </code></pre>
  
  <p>I am using a polymorphic relationship to attach colors to cats (and other creatures). I have a <code>colorables</code> table:</p>
  
  <pre><code>id | color_id | colorable_id | colorable_type
  ---------------------------------------------
  1  | 1        | 2            | 'App\Cat'
  </code></pre>
  
  <p>and in my <code>Cat</code> model I have</p>
  
  <pre><code>public function colors()
  {
    return $this-&gt;morphToMany('App\Color', 'colorable');
  }
  </code></pre>
  
  <p>I have this controller code to get all cats of a given color:</p>
  
  <pre><code>public function index(Request $request)
  {
    $cats = new Cat;
    if ($request-&gt;has('color')) {
      $color = $request-&gt;color;
      $cats-&gt;whereHas('colors', function ($query) use ($color) {
        $query-&gt;where('color_id', $color);
      });
    }
    return $cats-&gt;get();
  }
  </code></pre>
  
  <p>But it's returning all cats irrespective of their color.</p>