PHP Laravel:var的类型从本地到服务器不同 [英] PHP Laravel: Type of var is different from local to server

查看:90
本文介绍了PHP Laravel:var的类型从本地到服务器不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel应用程序中遇到了一个奇怪的错误.

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

模型x的属性status在localhost中是一个整数,但在我的生产服务器中是一个字符串.

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

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

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

因为我使用严格的比较,这在我的应用程序中引发了错误.

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

在MySQL和PHP 5.6上均使用Laravel Framework 5.4.1.

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?

推荐答案

这取决于在php和mysql之间使用的驱动程序.

It depends on the driver used between php and mysql.

通过检查

php -i

您的输出应类似于

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.

或在模型中使用$casts.

    protected $casts = [
    'status' => 'integer',
];

这篇关于PHP Laravel:var的类型从本地到服务器不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆