return response->json($array) returns Integers and Floats as "Strings"

Posted 1 year ago by Notdavis

Array is created from a DB::table query, and oddly the first value, id, isn't converted, all the rest are. I thought it might have something to do with the name so I tried changing it to ralph to see, and it still comes through as an int.

I've got my columns cast as integers, and even made sure the table in the DB::table is the one used by model with the casts. However, I'm not sure that matters as the values pass through a foreach loop and are assigned to another vanilla array.

In the foreach I've even tried is_numeric to intval() and that doesn't work.

The one thing that does work is encoding it to json with JSON_NUMERIC_CHECK then decoding, then passing it to response->json() to have it encoded again :

 $response = json_encode(array('status' => 'success', 'data' => $response), JSON_NUMERIC_CHECK);
 $response = json_decode($response);

 return response()->json($response);

What did I miss?

Please sign in or create an account to participate in this conversation.