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

Asked

Viewed 102 times

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 when choosing any of these parameters, you can choose its value in a list (with database values).

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

Example:

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

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

What would be the solution to this?

1 answer

1


The problem is that you are using the same field several times to receive different values. One way to solve this is to use only one field receiving 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 make 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();

}
  • Thanks @Kayo-Bruno, I solved with this same idea only in a different way!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.