简体   繁体   中英

PHP Laravel: Type of var is different from local to server

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?

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',
];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM