Laravel dynamic where clauses
<p>I would like to create a dynamic query with laravel ORM. In my example:</p>

<pre><code> $query = Task::orderBy('customer_family_name');

 if (array_key_exists('date', $request-&gt;filter)) {
     if($request-&gt;filter['date'] != null) {
        $query = $query-&gt;whereDate(....);
     }
 }

 ..... more filters


 if (array_key_exists('search', $request-&gt;filter)) {
       if(strlen($request-&gt;filter['search']) &gt; 1) {
            $searchText = $request-&gt;filter['search'];
            $query = $query-&gt;where('customer_family_name', $searchText.'%');
            }
        }
 }

 $Tasks = $query-&gt;get();
</code></pre>

<p>But it is not working as expected. I thought that laravel will push all where elements, so that ALL where clauses must be met. </p>

<p>But the search for a family name is not working. Do i something wrong? How can i combine it correct? Is there a way to display the SQL Query with orm? </p>

<p><strong>Edit: ok found out that is another problem, my query looks like:</strong></p>

<pre><code>"select * from [tasks] where [state_id] in (?, ?, ?, ?, ?, ?) and [customer_family_name] = ? and [regulator_user_id] in (?, ?, ?) or [regulator_user_id] is null order by [customer_family_name] asc"
</code></pre>

<p>And my filter:</p>

<pre><code>if (array_key_exists('regulator', $request-&gt;filter)) {

      $regulator_array = explode(",", $request-&gt;filter['regulator']);

      if(is_array($regulator_array)) {
                $query-&gt;whereIn('regulator_user_id', $regulator_array)-&gt;orWhereNull('regulator_user_id');
       }

  }
</code></pre>

<p>It looks like that the orWhereNull is the problem. I would like to return every row where </p>

<p>regulator_user_id is in that array
AND all where regulator_user_id IS NULL</p>

<p>But all other filters should work as expected.</p>

<p>How can i do that? </p>