Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

On my development server the JSON response from the Laravel 5 controller shows the data in the correct types.

e.g

imdb_rating: 7.6
imdb_votes: 6271

But on the production server, the JSON response is sent back as strings.

imdb_rating: "7.60"
imdb_votes: "6271"

Both development and production have the same version of PHP installed (5.6.11-1).

Any ideas on what may be causing this behaviour?

See Question&Answers more detail:os

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

1 Answer

I just ran into this same issue! For those of you looking for a more appropriate solution, you might want to check out the $casts property for Eloquent models.

The accepted solution will work, but it will also convert fields that you may not want converted. I would recommend adding this to your Eloquent model:

protected $casts = [ 'imdb_rating' => 'float', 'imdb_votes' => 'integer' ];

This will convert the values directly in your model object, so you won't need to worry about updating multiple endpoints. I hope this helps others as it did me!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...