Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
576 views
in Technique[技术] by (71.8m points)

php - find() on model gives id as string in one environment and int in other

I am using Laravel 5 and have a model Book.

I have two environments - dev and production

On dev: In tinker, when I do

Book::first()

It gives me

AppBook {
     id: 1,
     name: ...

On produvction: The same thing gives me

AppBook {
         id: "1",
         name: ...

Notice the id is int in dev but string on production

Because of this, a condition like

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

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

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

PS:

  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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...