0

I use the following command to output in Laravel :

response()->json($data, $status, [])->send();

it has been converting all the numbers that are numerically defined in the database into strings Using the following value solves this problem, but this solution converts all numbers into numeric types

response()->json($data, $status, [], JSON_NUMERIC_CHECK)->send();

And I do not want this to happen because there are numbers in the database that are from the string and should remain the string.

What should be done to display all values of their type in the output?

  • You are going to have to show some examples, I think. For example, do you have two columns, one which is a varchar that holds something like `"7"`, and another that is an int that holds something like `3`, and only the latter should be converted to a number? If that what you mean? – Chris Haas Aug 31 '20 at 17:22
  • 1
    as @chris says your question is a bit confusing. But if you are using models for your query what I think you need is casting. take a look [here](https://laravel.com/docs/7.x/eloquent-mutators#attribute-casting) – Jairo Nava Magaña Sep 01 '20 at 17:56

1 Answers1

1

Yep, as Jairo Nava said;

In your model

  protected $casts = [
    'your_int_field' => 'integer',
  ];

With a correct INT sql field.

Patoumpalou
  • 86
  • 1
  • 5