How to filter a list using the same parameter with two different values?

1

I'm implementing a motion filter where you can choose the parameter on which the filter should be based (for example: Date, Species, User) and choose any of these parameters, the value of it in a list (with database values).

The point is that these inputs can receive multiple values, but it considers only one of these values ...

Example:

/consultas/movimentacao-residuo?cd_especie=1&cd_especie=2

In this example it will consider the first parameter, but the second one will not.

What would be the solution to this?

    
asked by anonymous 21.06.2017 / 19:52

1 answer

1

The problem is that you are using the same field multiple times to receive different values. One way to solve this is to use only one field by getting all values separated by some character.

For example:

// Nesse exemplo utilizo a virgula como separador 
/consultas/movimentacao-residuo?cd_especie=1,2,3

In the function you will receive the values you do the following treatment.

public function filtrarMovimentacoes( Request $request ) 
{
    // Você vai ter um array de IDS
    $cd_especie = explode( ',', $request->input('cd_especie') ); 

    // Agora é só utilizar para filtrar o que você quiser
    // whereIn recebe como segundo parâmetro um array
    DB::table('movimentacoes')
                ->whereIn('cd_especie', $cd_especie) 
                ->get();

}
    
22.06.2017 / 03:54