1

I am using Laravel3 Response::eloquent.

The result is in JSON format: {"id":"1"}. However I want the result like this: {"id":1} with id as a number, not string type.

I use apache and Content-Type is application/json; charset=UTF-8.

The problem only occurs on the production system. On my localhost the result is {"id":1}.

Is there any relevant setting for Apache or Laravel3 ?

рüффп
  • 5,172
  • 34
  • 67
  • 113
Jin Wang
  • 27
  • 7
  • If you are really experiencing different ways in your localhost and in production, I would recommend you do a `JSON.parse(responseText);`. That will ensure you will get the thing corrected all ways. – Siva Tumma Dec 25 '13 at 12:24
  • I'm useing angular in frontend, and I'm sure it not js issue – Jin Wang Dec 25 '13 at 12:30
  • @sivatumma `JSON.parse();` is a JS function and not PHP. – user555 Dec 25 '13 at 12:31

2 Answers2

2

This is most likely due to PHP and in turn eloquent using the wrong database driver. Starting from PHP 5.3 mysqlnd is the default database driver with support for returning native data types.

Make sure that your using the mysqlnd driver for MySQL.

Also see How to get numeric types from MySQL using PDO?.

Community
  • 1
  • 1
user555
  • 1,564
  • 2
  • 15
  • 29
1

I agree with @user555. It is not a Laravel 3 or Laravel 4 issue, a mysqlnd issue, though.

Due to the wrong mysql client library your fetching table field types as strings then json_encode() is getting "string" as input and encoding to "string" as well.

I wrote an article about my experiencie on that here:

http://www.jmilan.net/posts/json-encode-data-type-errors-in-php Hope it helps

jhmilan
  • 508
  • 3
  • 12