ScriptMint's avatar

Laravel API response return double quoted numeric value

Laravel Version: 5.6.12 PHP Version: 7.1 Vuejs Version: 2.5 Database Driver & Version: MySQL 5.7 (Using mysqlnd driver)

Description: API response returning double quoted numeric value therefore producing undesired output. The response recieved from the server is attached in this image:

return response()->json($data, 200);

Here is the response received from the server (Screenshot)

https://user-images.githubusercontent.com/31760604/37862568-1b8e1cba-2f75-11e8-8d25-140d9db1f8f5.jpg

Why numeric values in response is double quoted?

Steps To Reproduce: A simple post request to http://task-king.online/api/auth/check

0 likes
3 replies
zymawy's avatar

You Could Use API Resources

https://laravel.com/docs/5.6/eloquent-resources

Or You Could Casts The All Attributes You Want To It To Be Numeric In Your Model


    protected $casts = [
      'https',
      'mode',
      // And So On 
    ];

Helmchen's avatar
Helmchen
Best Answer
Level 15

PHP's json_encode has an flag for this: JSON_NUMERIC_CHECK i'am not sure what laravel uses for json encoding but there might be an option.

i would probably go for a parseInt() on the client side and take it like it is

edit: found it

return Response::json($data, 200, [], JSON_NUMERIC_CHECK);

or even better

return response()->json($data)->setEncodingOptions(JSON_NUMERIC_CHECK);

Please or to participate in this conversation.