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

Ligonsker's avatar

Adding commas to SUM in raw query

I have a few columns that I caclculate their SUM using raw query:

MyModel::select(DB::raw(SUM(col1) as col1_sum, SUM(col2) as col2_sum)))->groupBy('col1_sum', 'col2_sum')->get();

Is it possible to add commas whenever needed (every 3 digits from the right if exist)

It is going to be displayed in Blade. Is my best option to use a new Blade directive, or there's a better option?

Ty

0 likes
4 replies
tykus's avatar
tykus
Best Answer
Level 104

PHP's number_format, no?

{{ number_format($model->col1_sum) }}
1 like
Ligonsker's avatar

@tykus Oh yes 😅 thank you! Is it possible to do it on the entire array?

Edit: Nvm, I might need to use map

Ligonsker's avatar

@tykus Yes, (Just the way the code is written I will need to do it manually for many rows now :D )

Please or to participate in this conversation.