Laravel query returns "wrong" result

Asked

Viewed 51 times

1

I have a table called "tabela_precos", with the following structure:

categoria_veiculo_id | valor | preco_min | preco_max | tipo_veiculo
22                     77,70   0           2500        carros  
22                     30,35   15000       20000       carros

When I run the following query in phpmyadmin, the return is correct and only brings a result (the register with a value of 30,35):

select * from `tabela_precos` where `tipo_veiculo` = "carros" and `categoria_veiculo_id` = 22 and 15000 between preco_min and preco_max ;

but when I run this on Laravel, it returns me the two records (even using limit 1 or first(), code:

$query = TabelaPreco::whereRaw('? between preco_min and preco_max', [$request->preco_fipe])
        ->where('categoria_veiculo_id', $request->categoria_veiculo_id)
        ->where('tipo_veiculo', $request->tipo_veiculo);

this data comes from a form, and even if I put the data directly into the code, the same two records are returned.

I’ve already given one toSql() and the query is exactly the same that I ran directly on phpmyadmin

I know this is probably something very silly, but I couldn’t understand why it happens.

1 answer

0

After running all over the internet looking for this, it turned out the mistake was that I was sending the values of preco_fipe as string and so it generated this "strange" result, when I converted to int the results came right

  • I didn’t understand why my answer was negative, no one could answer, it’s a subtle detail but difficult to understand...

Browser other questions tagged

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