title: How can I Refactor this search filter query?
body: <p>I need to search in multiple tables. I'm checking every single request object. For example: I am checking if the request has that object then concating it to my main query and getting that query result at the last. It doesn't looks and isn't good. How can I make this search query filter better in laravel? 
  <strong>Note: I have searched questions in stackoverflow but they are dealing with only one model.</strong></p>
  
  <pre><code>$query = DB::table('clients')
          -&gt;leftjoin('ecommerce_contacts','ecommerce_contacts.client_id', '=', 'clients.id')
          -&gt;select('ecommerce_contacts.*', 'clients.*')
          -&gt;where('clients.is_deleted', '=', '0');
  
          if(!is_null($request-&gt;fname)){
              $query+=-&gt;where('clients.fname', 'like', '%$request-&gt;fname%');
          }
          if(!is_null($request-&gt;lname)){
              $query+=-&gt;where('clients.lname', 'like', '%$request-&gt;lname%');            
          }
          if(!is_null($request-&gt;gender)){
              $query+=-&gt;where('clients.sex', $request-&gt;sex);            
          }
          if(!is_null($request-&gt;number)){
              $query+=-&gt;where('ecommerce_contacts.sex', 'like', $request-&gt;number);            
          }
          if(!is_null($request-&gt;registered_date)){
  
          }
          if(!is_null($request-&gt;purchase)){
  
          }
          $client = $query-&gt;get();        
          $data = json_encode($clients);
          return $data;
  </code></pre>