Pete
Pete

Reputation: 7579

Eloquent returning unpredictable result with ->where()

In one of my controllers, I have the following code:

$sim = 8944502311119819084;
$firstDevice = Device::where( 'sim', $sim )->first();

$sql = "SELECT * FROM devices WHERE sim = ?";
$secondDevice = DB::select($sql, [$sim]);

Log::debug($firstDevice);
Log::debug($secondDevice);

Both are returning a device with a sim number that doesn't match, so it's like the where() is being ignored.

How can this be? Is there any known explanation for this behavior? Can Eloquent somehow be using a different database connection (I mean, I'm 99% it's not, but can I rule that out altogether), or could the Device scope be polluted somehow?

Edit:

If I do DB::getQueryLog() on the first request, I get:

array (
  0 => 
  array (
    'query' => 'select * from `devices` where `sim` = ?',
    'bindings' => 
    array (
      0 => 8944502311119819084,
    ),
    'time' => 0.76,
  ),
)  

Edit #2;

Okay, technically my initial code example showed $sim = '8944502311119819084';. In my real situation, that sim number is coming in via the route. And it's coming in as an int—which makes all the difference in the world. If I switch it to string, it works fine. So now I've switched my example to use it as an int, which actually breaks both situations now.

I would think it's related to the max value for integers in PHP, but I've verified that I'm on a 64-bit system with 64-bit PHP, which has a max integer size of: 9,223,372,036,854,775,807. That's higher than the sim:

Sim: 8944502311119819084
Max: 9223372036854775807

Is there some other reason that an integer that high wouldn't be supported by MySql or something?

Upvotes: 1

Views: 72

Answers (1)

Flame
Flame

Reputation: 7570

Im guessing this depends on the way your sim is stored, take a look at this:

https://dev.mysql.com/doc/refman/8.0/en/integer-types.html

Taking into account signed or unsigned columns (I assume yours is signed which is the default), you need your column to be BIGINT, resulting in 2^63 -1 As the largest integer.

But really it might be just simpler to do where('sim', (string) $sim)

Upvotes: 1

Related Questions