Why SUM(`column`) returns a string instead of an integer?

It's neither Laravel or PDO issue.

According to the manual, SUM() returns a DECIMAL value for exact-value arguments (integer or DECIMAL). And the only way to represent DECIMAL type in PHP is string, for two reasons:

  • it can overflow the PHP's int type, being bigger than PHP_INT_MAX.
  • also, in case the returned value being a decimal number, it can lose precision due to inherently imprecise nature of floating point numbers

due to these precautions the returned value is a string and you are supposed to convert it manually according to the expecting value - either using standard PHP type casting or using some dedicated math functions such as bcmath.

Tags:

Mysql

Php

Pdo