javascript - boolean in vuejs becomes 1 on server -


i sending value frontend backend , value boolean. when on server this

dd($request); 

i can see receive true, example:

      "displayslug" => array:2 [         "type" => "boolean"         "name" => true       ] 

later when loop through array of parameters page, this:

dump($key .': ' . $value['name'] .'; type: '.$value['type']); 

now prints this:

"displayslug: 1; type: boolean" 

when save db, gets saved number , not boolean, becomes 1.

$setting = new setting();  $setting->key = $key;  $setting->type = $type;  $setting->value = $value;  $this->settings()->save($setting);  return true;  

value boolean gets stored 1, i'd store true if string in db

i boolean in db. can boolean instead of number?

column saving string column.

when printing/dumping boolean value in php, gets cast string.

a boolean true value converted string "1". boolean false converted "" (the empty string). allows conversion , forth between boolean , string values.1

you convert string boolean:

$value = (bool)$valuefromdatabase; 

bear in mind:

when converting boolean, following values considered false

instead of converting bool explicitly, 1 use truthy/falsey coersion of strings (refer php type comparison tables more information).

$valuefromdatabase= 0; //false  if (!$valuefromdatabase) { //0 falsey, negated yields truthy expression       //action take server-side when value false } $dataforjsonoutput = array('value' => $valuefromdatabase); //use in javascript code - e.g. vuejs echo '<script type="text/javascript">var values = '.json_encode($dataforjsonoutput).'; </script>; 

see demonstration in this playground example


1http://php.net/manual/en/language.types.string.php#language.types.string.casting 2http://php.net/manual/en/language.types.boolean.php#language.types.boolean.casting


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -