在模型上的find()在一个环境中将id作为字符串提供,在另一个环境中将int提供 [英] find() on model gives id as string in one environment and int in other

查看:60
本文介绍了在模型上的find()在一个环境中将id作为字符串提供,在另一个环境中将int提供的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel 5,并具有模型Book.

I am using Laravel 5 and have a model Book.

我有两个环境- dev production

dev 上: 在tinker中,当我这样做

On dev: In tinker, when I do

Book::first()

它给了我

App\Book {
     id: 1,
     name: ...

关于产品: 同样的东西给了我

On produvction: The same thing gives me

App\Book {
         id: "1",
         name: ...

注意id dev 中是int,但在 production

因此,类似

if($id === $book->id)

dev 上正常工作的

生产上不工作.

which works correctly on dev, doesn't work on production.

有人知道为什么会这样吗?有没有一种方法可以使我的 production 生产环境给我int而不是string?

Any idea why this is so? Is there a way I can make production environment to give me int instead of string?

PS:

  1. 我已经在 dev 上安装了Laravel,然后通过 git production 上提取了代码.因此,我的composer.jsoncomposer.lock dev production 上完全相同.因此,在 production 上运行composer install应该会为我提供与 dev 上相同的环境.
  2. 我正在使用mysql数据库.
  3. 我的 dev 环境是Laravel的homestead.
  4. Book只是我在这里使用的虚拟模型.我所有的模型都具有与上述相同的行为.
  1. I have installed Laravel on dev and then pulled the code on production via git. So my composer.json and composer.lock are exactly same on dev and production. So running composer install on production should give me same environment there as on dev.
  2. I am using mysql database.
  3. My dev environment is Laravel's homestead.
  4. Book is just a dummy model I've used here. All my models behave in the same manner as explained above.

推荐答案

很明显,您不想按照注释中的建议进行任何手动转换.似乎在一台服务器上您错过了 mysqlnd 扩展名(我需要假设您使用MySQL,因为您没有在问题中提到它)是什么原因导致所有数据类型都以字符串形式返回.

Obviously you don't want to do any manual casting as suggested in comment. It seems that on one server you miss mysqlnd extension (I need to assume you use MySQL because you haven't mentioned it in your question) what causes that all data types are returned as strings.

这篇关于在模型上的find()在一个环境中将id作为字符串提供,在另一个环境中将int提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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