title: laravel append wheres in a loop to perform AND filtering
body: <p>I'm trying to build my own dynamic filtering for my Angular App and Laravel 5.1 API using <code>$httpParamSerializer(params);</code> and Laravel <code>whereIns</code>.</p>
  
  <p>My goal is to pass a set of fields and values I want to filter on and drill down the records I need.</p>
  
  <p>I loop through the fields in Laravel and perform <code>whereIns</code>, then group them into an array, removing duplicates. Unfortunately, this acts more as an <strong>OR</strong> than an <strong>AND</strong>, as each <code>whereIn</code> does a new search to match.</p>
  
  <p>Frontend Query:</p>
  
  <pre><code>        var filters = {
              'review[]' : ['no', 'yes'],
              'user_id[]' : [1]
          };
  </code></pre>
  
  <p>HTTP URL: "<a href="http://dde-api.localhost/1.0/userquestions?review%5B%5D=no&amp;review%5B%5D=yes&amp;user_id%5B%5D=1" rel="nofollow noreferrer">http://dde-api.localhost/1.0/userquestions?review%5B%5D=no&amp;review%5B%5D=yes&amp;user_id%5B%5D=1</a>"</p>
  
  <p>DB:</p>
  
  <p><a href="https://i.stack.imgur.com/8fSslm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8fSslm.png" alt="enter image description here"></a></p>
  
  <p>Laravel:</p>
  
  <pre><code>    $results = [];
      // Loop through each field (`review`, `users`, etc..), then search thru array of params
      foreach ($input as $filter_field =&gt; $filters_array) {
          if ($this-&gt;validField($table, $filter_field)) {
              $res = DB::table($table)-&gt;whereIn($filter_field, $filters_array)-&gt;get();
              if (!in_array($res, $results)) {
                  array_push($results, $res);
              }
  </code></pre>
  
  <p>I need the query to work as a multiple <code>WHERE</code> clause (<strong>AND</strong>, not <strong>OR</strong>) which loops through and appends <code>where</code> clauses to each <code>field</code> ($filter_field), then searches for matching field values.  </p>
  
  <p>So the result should be all <code>yes</code> and <code>no</code> records for user <code>1</code>. Since user 1 doesn't have a record with <code>review: yes</code>, it uses the record from <code>user_id: 4</code>. This is bad.</p>
  
  <p>How can I append multiple <strong>WHERE</strong> statements to one query as I loop through multiple fields?</p>