I have a Laravel application. In the users
table, I have a boolean
column called active
:
$table->boolean('active')->default(false);
And in the front-end I normally check to see if the user is active or not like this:
Plain JS: if(user.active){ // do stuff }
VueJS: v-if="user.active"
This approach assumes that the value of user.active
property, which of course is coming from the json response, is a non-string value: either 0 or 1. And that's what indeed I am receiving in the local machine:
Here pink colored values represent String values and green colored values represent Number values. So, clearly, my local machine is interpreting it as a Numeric value and when the number 0 or 1 is used in a conditional expression, it is converted to boolean.
However that's not the case on my production server. The json response in the production server is converted to string which leads to unintended behavior of my application:
Notice that the value for the active
key is not a Number, it is a String here! And since it's interpreted as a String, any non-empty string will always evaluate to true
; hence my condition will always evaluate to true
.
I have two questions: a) Why is this happening? b) How can I fix it?
Additional information: Both the production and local server are running same mysql version. PHP version on prod: 7.2.8 and PHP version on local: 7.2.9.