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

£可爱£侵袭症+ 提交于 2019-12-12 04:32:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!