title: Laravel - Query nested relationship with Laravel when clause
body: <pre><code>// This comes in as an array - item_category[]
  $category = \Request::input('item_category'); 
  
  Item::with('category')
      -&gt;when($category, function($q) use ($category) {
          $q-&gt;whereHas('category', function($q) use ($category) {
              $q-&gt;whereIn('category', $category);
          });
      })-&gt;get();
  </code></pre>
  
  <p>Hello, I am trying to retrieve all items where the category equals the any of the categories that is passed through. However, the category is a filter and the user may or may not check this, hence the when clause chained to the model. The code currently throws an error </p>
  
  <pre><code> Column not found: 1054 Unknown column 'has' in 'where clause' (SQL: select * from `items` where `has` = category)
  </code></pre>
  
  <p>If i do</p>
  
  <pre><code>    Item::with('category')
      -&gt;whereHas('category', function($q) use ($category) {
              $q-&gt;whereIn('category', $category);
          })-&gt;get();
  </code></pre>
  
  <p>This will work, except if the user doesn't check any of the item_category checkboxes, the query will return 0 results since it is expecting this value to pass through. 
  I really don't know what do here. Any advice. 
  Regards,</p>
  
  <p>items table </p>
  
  <pre><code> id   category_id   title   description   price   timestamps
  </code></pre>
  
  <p>categories table</p>
  
  <pre><code> id   category
  </code></pre>
  
  <p>Item.php</p>
  
  <pre><code> public function category()
   {
      return $this-&gt;belongsTo('App\Category', 'category_id');
   }
  </code></pre>