It depends on the php database driver. The pdo_mysql extension returns them as strings. mysqlnd (mysql native driver) returns them as they are (ints as ints, strings as strings, etc). It's not actually laravel or eloquent doing this.
How are numeric columns casted automatically to integer?
I could bet $100 that it wasn't the case in the past. If you have a model with column of the type INT, FLOAT, DOUBLE (and any variations, like TINYINT etc.), it will automatically be casted to integer in Eloquent. But this is not the case for DECIMAL. Let's say I have an App\Dog model with legs_count column of type INT in the database.
If you do:
$dog = \App\Dog::firstOrFail();
var_dump($dog->legs_count);
It will give you int 4
If you change the type of column in the database directly from INT to VARCHAR, the result is immediately different: string '4'
How does it happen? I could swear that in the past everything was returned as a string by default. I was thinking that maybe a new PDO version now returns numbers somehow, but no - it just returns strings.
What is going on here?
Please or to participate in this conversation.