1

I've recently updated my server to a newer version of MySQL and PHP 7 for various reasons. On my previous instance, running PHP 5.5, Laravel's response()->json() always converted tinyint's into a string. Now running newer server software, it's returning me int's -as it should...

I'd have to change a lots of my codebase to either cast types / convert them into a string manually, whic I'm trying to avoid at the moment.

Is there a way to somehow force response()->json() to return int's as string's?

6
  • 1
    PHP is very loose about types so even its int you can use as string except if you are using ===. Commented Mar 31, 2017 at 12:06
  • 1
    you did not understand the question I am afraid Commented Mar 31, 2017 at 12:07
  • 1
    I don't really know Laravel, but could it be possible to override the native json() function and use settype() in it ? This way you won't have to change all your function calls. Commented Mar 31, 2017 at 12:21
  • Yes, I'll look into the method and try to override it. Thanks. Commented Mar 31, 2017 at 12:22
  • there are a workaround to do this, jsonp Commented Mar 31, 2017 at 12:53

5 Answers 5

2

Is there a way to somehow force response()->json() to return int's as string's

I don't want to change the code base - do not want to cast types, convert it,

No. There's no option for that. You need to do that yourself if needed.

Sign up to request clarification or add additional context in comments.

4 Comments

Maybe with some MySQL / PHP reconfiguration, maybe/hopefully? :(
No. There's no switch for that. It makes no sense. Not sure what you need that behavior for too
The app where I'm expiriencing this is an API, and it's used by a mobile (iOS / Android) application which handles everything as string, not since Swift and Java are both strict-typed langies, this is causing a crash on both of them...
Fix the app then. Not sure how exactly you parse the JSON in apps, but it smells like it's simply done wrong way.
2

There is a way to cast integer into string in laravel

in your model you can cast id to string. Its as follows

protected $casts = [ 'id' => 'string' ];

But the downside is that you would have to do that for all Models.

Comments

0

If you don't want to modify a lot of code you could run response data through a quick and dirty function. Instead of going directory to JSON you should instead grab the data as a nested array. Then put it through a function like this:

function convertIntToString ($myArray) {
    foreach ($myArray as $thisKey => $thisValue) {
        if (is_array($thisValue)) {
            // recurse to handle a nested array
            $myArray[$thisKey] = convertIntToString($thisValue);
        } elseif (is_integer($thisValue)) {
            // convert any integers to a string
            $myArray[$thisKey] = (string) $thisValue;
        }
    }
    return $myArray;
}

The function will convert integers to strings and use recursion to handle nested arrays. Take the output from that and then convert it to JSON.

1 Comment

I wrote quite a similar recursive function as you did... but I'll avoid that for now. Thanks anyway.
0

The best solution for me is to to use attribute casting and Fractal transformers

Fractal transformers are extremely useful when you have complex responses with multiple relations included.

Comments

-1

You can typecast it to string:

return response->json(["data" => (string)1]);

1 Comment

As I said - I don't want to change the code base - do not want to cast types, convert it, etc...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.