0

I've encountered a strange bug in my Laravel application.

A property status of a model x is an integer in localhost, but is a string in my production server.

"status" => 1 "status" => "1"

This throws an error in my application because I'm using strict comparison.

Both use Laravel Framework 5.4.1 on PHP 5.6, with MySQL.

So I have no idea where the difference comes from... Do you?

Jachinair
  • 272
  • 6
  • 15
  • I can only assume that you are casting `status` to a string within model `x`. `protected $casts = [ status' => 'string', ]`. Something like that maybe? – Jack Mar 14 '17 at 16:42
  • @Jack Thanks for your reply, but no, I'm not casting anything... Oh, if that was a suggestion to do, I might do that, yes. But I don't understand where the difference come from and it could bring more issues... – Jachinair Mar 14 '17 at 16:45
  • Yeah agreed that it shouldn't happen if the code base is exactly the same. I would destroy the server and spin another one up to see if there was anything wrong. – Jack Mar 14 '17 at 16:53

1 Answers1

2

It depends on the driver used between php and mysql.

Check which one of them is used by checking pdo_mysql section of the output of

php -i

your output should be similar to

pdo_mysql

PDO Driver for MySQL => enabled
Client API version => mysqlnd 5.0.12-dev - 20150407 - $Id: b396954eeb2d1d9ed7902b8bae237b287f21ad9e $

The native driver return integers as integers, but the other return them as strings.

So the solution is to remove the old driver and install the native one.

or to use $casts into your model.

    protected $casts = [
    'status' => 'integer',
];
Shady Atef
  • 2,121
  • 1
  • 22
  • 40
  • Thanks for your reply. On locahost, I have `mysqlnd 5.0.11-dev - 20120503 - $Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $` while on the server I have simply `5.5.54`. If my property is an int on localhost, that means `mysqlnd 5.0.11-dev - 20120503 - $Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $` is the native driver, correct? Do you know where the driver on production come from and why I do not have the same? I don't think I installed anything facy. I'll look at I can remove a driver now. – Jachinair Mar 14 '17 at 17:12
  • Yes `mysqlnd` is the native driver. I don't know how your production server is set-up, but if you have access to the package management you can search for the native driver and install it. `sudo apt-get install php5-mysqlnd` if on ubuntu-based – Shady Atef Mar 14 '17 at 17:16
  • Installed and the problem is now fixed. Thank you. – Jachinair Mar 14 '17 at 17:22
  • Unfortunately, the issue is back when using PHP 7.1: http://stackoverflow.com/questions/42862620/db-returns-string-instead-of-int-mysqlnd-for-php-7-1-laravel – Jachinair Mar 17 '17 at 16:28