framework: Eloquent whereRaw method incorrectly binds floats

  • Laravel Version: 6.11.0
  • PHP Version: 7.3.13
  • Database Driver & Version: SQLITE

Description:

whereRaw method binds float values as strings.

Steps To Reproduce:

Provided a stations table with a REAL lat column: Station::whereRaw('lat > ?', [40.0])->get() will result in SQL: select * from "stations" where "lat" > "40.0" which clearly doesn’t always work.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (13 by maintainers)

Most upvoted comments

As noted by @mo3000 , this seems to be a limitation on PHP’s PDO itself. PDO doesn’t have a parameter type for floats:

https://www.php.net/manual/en/pdo.constants.php

There was a RFC for adding it but it was withdrawn:

https://wiki.php.net/rfc/pdo_float_type

So one cannot use PDO to bind a float value without it being converted to a string.

One solution might be to add 0.0 so SQLite converts the binding to a float before comparing:

Station::whereRaw('lat > (? + 0.0)', [40.0])->get()

Didn’t tested it though, hope it helps.

Thanks for explaining @rodrigopedra.

@slavicd please see the answer above.