i am using
$user = User::find($user_id);
The difference in the type is most likely due to the fact that your local server is using the
php5-mysqld (non-native) driver, whereas your production server is probably using the
php-mysqlnd (native) driver. One of the main differences is that the non-native driver reads all fields as strings, whereas the native driver will automatically convert integer fields to PHP integers.
id shows up as an integer in both cases because Laravel automatically adds the primary key field ('id') to the casts array with the type defined by the
$keyType property (default to
I would say you have four options:
Attempt to install the non-native mysql driver for PHP 7 on your production server (not recommended).
Don't do anything on the PHP side, and just make sure that whatever is consuming the json can handle strings or integers (probably a good idea either way, but still doesn't solve the real issue).
postal_code to your
casts array, to ensure it is always casted to a string (not bad; this will work for most cases, but is not exactly the correct solution).
Change the field type of the
postal_code field from an
integer to a
varchar(10), depending on if you're going to store the +4, and with or without a hyphen (best solution).
postal_code data type in the database to a
varchar really is the best option. The main reason is that, while they are comprised of numbers, postal codes are not actually integers, or numeric in nature. You will never be doing any arithmetic with them. Additionally, there are postal codes that start with a leading 0, and if the value is stored as an integer, this is an extra edge case condition you have to contend with whenever displaying postal codes.