How to Filter Multiple Fields in Query?

6

I need to filter a query in Laravel.

How can I do this in the best way? For example, I have a query on my page and I want to do some filtering on it. For this I have a column that has some filter options.

How would I do this in the code? I thought so:

if($string)
   $consulta = Model::where('nome', 'like', '%'.$consulta.'%');

if($numero)
   $consulta = Model::where('matricula', '=', $numero);

if($outro)
   $consulta = Model::where('outro', 'like', '%'.$outro.'%');

And then in the end I would make the query. Is there any better way?

    
asked by anonymous 21.05.2016 / 17:28

2 answers

5

Yes you can do this by using the where(function(){}) method as follows:

For Laravel 4.2

$consulta = Model::where(function($query) use($string, $numero, $outro) {

     if($string)
         $query->where('nome', "like", "%{$consulta}%");

     if($numero)
         $query->where('matricula', '=', $numero);

      if($outro)
         $query->where('outro', "like", "%{$outro}%"); 
})
->paginate(20);

or

$consulta = Model::where(function($query) {

         if(Input::has('consulta'))
             $consulta = Input::get('consulta'); 
             $query->where('nome', "like", "%{$consulta}%");

         if(Input::has('matricula'))
             $numero = Input::get('matricula');
             $query->where('matricula', '=', $numero);

          if(Input::has('outro'))
             $outro = Input::get('outro');
             $query->where('outro', "like", "%{$outro}%"); 
    })
   ->paginate(20);

for laravel 5.2

$consulta = Model::where(function($query) use($request) {

         if($request->has('consulta'))
             $consulta = $request->consulta; 
             $query->where('nome', "like", "%{$consulta}%");

         if($request->has('matricula'))
             $numero = $request->matricula;
             $query->where('matricula', '=', $numero);

          if($request->has('outro'))
             $outro = $request->outro;
             $query->where('outro', "like", "%{$outro}%"); 
    })
    ->paginate(20);
    
24.05.2016 / 18:06
3

Petter, if the query logic is going to repeat, it does not have to fill the "stop" of if . I recommend you simplify your code by encapsulating query logic within a loop.

For example, you want the primeiro_nome , email , and sobrenome fields to be searched with LIKE and nivel_id , idade , and curso to be searched with the = / p>

You would not have to make multiple ifs , but you could do a foreach and apply the logic in there.

$callback = function ($query) use($request) {

    $likes = ['primeiro_nome', 'email', 'sobrenome'];

    foreach ($request->only($campos_like) as $name => $value)
    {
        $value && $query->where($name, 'LIKE', "%{$value}%");
    }


    $equals = ['nivel_id', 'idade', 'curso'];

    foreach ($request->only($campos_equal) as $name => $value) {

        $value && $query->where([$name => $value]);
    }

};

Usuario::orderBy('primeiro_nome')->where($calback)->paginate(15)->appends($request->all());

Note that it is not necessary to fill the code with if . If you can simplify, always simplify.

Maybe you're wondering, "But where are the ifs?"

If you look closely, I replaced if with expression $value && $query->where(...) . I usually do this when I want to simplify a if . But I will not go into many details, as I have answered this before:)

Refactoring is worth it if the soul is not small!

    
25.05.2016 / 14:28