A
A
Antonio Solo2019-12-09 14:04:35
MySQL
Antonio Solo, 2019-12-09 14:04:35

Multiple SUM in laravel (Eloquent, MySQL) - how to do it right?

It is necessary to SUM over a large number of fields, I do this

$qry = Model::where(...)-> .....
            ->addSelect($DB->raw('contractor_id, 
                sum(`credit`) as `credit`, sum(`income`) as `income`,  ......  sum(`active`) as `active`, 
            '));
        $balance = $qry->first();

Everything is good, except for one thing - numeric fields are cast as strings, which (when sent to JSON) subsequently spoils life.
5dee2c79106c1873957849.png
I did not think of anything better than explicitly converting all values.
The question is - is there a "correct" way to get around this problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question