title: How to make laravel query with search condition and filter condition from relational table
body: <p>I have models 'Case' and 'Type' in many to many relation. 'Case' belongs to a 'Type'. Now I am trying to implement search and filter in my case index. I have already included the search in the query builder so that the keywords match multiple columns and also columns in related table. Now I am trying to incorportae a filter also. But when I am including that filter in query builder, it is not working. Including either search or filter is working but including both together is not.</p>
  <p>Here is my code to include the search in query builder:</p>
  <pre><code>    $key = $this-&gt;search;
      $cases = Landcase::query()-&gt;orderBy('created_at', 'DESC');
  
      $cases
      -&gt;where('number', 'like', '%'.$this-&gt;search.'%')
      -&gt;orWhere('title', 'like', '%'.$this-&gt;search.'%')
      -&gt;orWhereHas('type', function ($query) use ($key) {
          $query-&gt;where('name', 'like', $key.'%');
      }); 
  </code></pre>
  <p>This much is perfectly working. I can search in title, number and type's name. Now here is the additional code to include the filter with type id(s) so that only specific type's cases will be shown and the search will work on those only.</p>
  <pre><code>  if($this-&gt;selected_types){
          $ids= $this-&gt;selected_types;
          $cases-&gt;whereHas('type', function ($query) use ($ids){
              $query-&gt;whereIn('id', $ids);
          });
      }
  </code></pre>
  <p>this block is not affecting the collection. But if I comment out the first block, this works. How can I incorporate it to my existing query builder block?</p>