Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Notdavis's avatar

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

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?

0 likes
8 replies
Notdavis's avatar

Late night mistake on my part. I was testing with 1 result, and I was interating over the returned rows, I was thinking I was interating over the columns. d'oh.

is_numeric and intval are now working.

In terms of query builder then, there is no easy way to cast values?

rumm.an's avatar

If you are using eloquent Models, you can use $casts property for casting attributes.

protected $casts = [
     'boolean_attribute' => 'boolean',
     'integer_attribute' => 'int',
];
Notdavis's avatar

Thanks for trying rumm.an, but it looks as if you didn't fully read the issue.

I've solved it for now using intval & floatval.

My question is, is this a possibility with Query Builder? Or if I go that route the onus is on me to make sure my ints are ints etc?

The wildcard is that first value int'n properly.

rawilk's avatar

@Notdavis - Maybe show us your query then. By the looks of it, $casts actually is what you need. There is no need for JSON_NUMERIC_CHECK and you definitely don't need to json_encode and then json_decode it before sending your response back.

Also, if you're using DB::query, don't. Use your model to query the database.

1 like
Notdavis's avatar

Sure, here is the query:

  $claimUpdates = DB::table('submitted_claims')
            ->join('claims', 'claims.id', '=', 'submitted_claims.claim_id')
            ->join('patients', 'patients.id', '=', 'claims.patient_id')
            ->join('submissions', 'submissions.id', '=', 'submitted_claims.submission_id')
            ->select('submissions.updated_at', 'submitted_claims.claim_id as id', 'submitted_claims.action', 'submitted_claims.paid', 'submitted_claims.message', 'submitted_claims.action_message', 'patients.last_name', 'patients.given_name', 'submitted_claims.accounting_num', 'submitted_claims.amount_paid', 'submitted_claims.paid_date', 'submitted_claims.recreate', 'submitted_claims.service_dates')
            ->where([
                ['claims.user_id', '=', $user->id],
                ['submissions.updated', '=', '1']
            ])
            ->get();

And as mentioned, on the SubmittedClaim model, the values I would like as Integers and Floats are in the protected $casts array, e.g.

protected $casts = ['batch_date' => 'date', 'paid_date' => 'date', 'recreate' => 'integer', 'paid' => 'integer', 'amount_paid' => 'float', 'action' => 'integer'];

Do the cast values types get applied to values returned from Query Builder?

Thanks, J

rawilk's avatar

I don't think they get cast if you use the DB facade. Use your model instead to do the query, with its proper relationships. There is no need to join like you're doing, when you could just use with.

You could rewrite it with eloquent something like this:

$claimUpdates = SubmittedClaim::with(['patients' => function ($query) {
        return  $query->select('last_name, given_name');
    }])
    ->with(['claims' => function ($query) use ($user_id) {
        return $query->where('user_id', $user_id);
    }])
    ->with(['submissions' => function ($query) {
        return $query->where('updated', 1)
            ->select('updated_at');
    }])
    ->select('claim_id as id, action, paid, message, action_message, accounting_num, amount_paid, paid_date, recreate, service_dates')
    ->get();

Also, is there any reason why you're selecting the columns manually like that?

1 like
Notdavis's avatar

Thank you, I will give this a try, and I will assume that Query Builder returns values ignorant of $casts and types. However, I still don't know why the first value (Id) was converted to an int while the rest wasn't with the data returned from the Query Builder, even when I renamed it form Id to Ralph.

In terms of why Query Builder, I'm used to writing SQL queries, and was doing so before moving to Laravel and I am learning Eloquent. I've also read Query Builder is a lot faster than Eloquent once you start joining multiple tables?

In terms of the select, this is part of a controller that returns only these values, and so I don't need the rest of the columns on all four tables that make up the join.

rawilk's avatar

I'm not really sure why id always gets converted, I think it's something that Laravel must be doing automatically to that column behind the scenes. In my experience, I haven't seen a difference with using eloquent and using joins when querying relationships in terms of performance; I've used with on a model with 5 or 6 relationships in one query and haven't noticed any kind of performance impact. A lot of it just depends on how well you have your schema set up. I also prefer using the model to query since I think it's more readable that way.

1 like

Please or to participate in this conversation.