Hello, thank you all for your support.
The issue is that I have a Laravel API already mounted on a hosting, and when I make a post request from my Android application I receive some data that is int and it returns them as a string "333", at first I thought it was the logic of part of the application but when making the request with postman I realized that it was actually from the api, I would like to point out that the same API locally does return the correct data, below I share a fragment of the response:
{
"paginate": {
"total": 9,
"current_page": 1,
"per_page": 15,
"last_page": 1,
"from": 1,
"to": 1
},
"products": {
"current_page": 1,
"data": [
{
"id": 51,
"name": "sfsdfsdf",
"price": "123",
"desc": "asdasdasd",
"unit": "paquete",
"img": "1606592605productoimage.png",
"created_at": "2020-11-28T19:43:25.000000Z",
"updated_at": "2020-11-28T19:43:25.000000Z",
"sin_delantal": "0"
},
WHAT MAKES ME CURIOUS IS THAT THE ID IS OF INT TYPE AND THIS DOES RETURN IT AS SUCH. The data that does not return correctly are: Price[int] and sin_delanta[tinyint], I would appreciate any help you could provide me.
Database queries generally return all columns as a string, regardless of the type defined when creating the table; but it has an easy solution: When you generate the array as JSON, simply add the JSON_NUMERIC_CHECK option to convert strings that represent numbers to numbers:
Result:
Reference: https://www.php.net/manual/es/function.json-encode.php#example-4490
After being stuck on this problem for 2 days, I found the solution in another question on StackOverflow in English:
Basically you have to add to the model of your class the $casts you need:
protected $casts = [ 'imdb_rating' => 'float', 'imdb_votes' => 'integer' ];
This way you can convert the response data to whatever day types you need. [Link to the original answer. https://stackoverflow.com/questions/31527050/laravel-5-controller-sending-json-integer-as-string]