问题
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?
回答1:
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',
];
来源:https://stackoverflow.com/questions/42791557/php-laravel-type-of-var-is-different-from-local-to-server