Reputation: 2977
I am sending a value from frontend to backend and this value is a boolean. When on the server I do this
dd($request);
I can see that I receive true, for example:
"displaySlug" => array:2 [
"type" => "boolean"
"name" => true
]
later when I loop through the array of parameters I get from page, like this:
dump($key .': ' . $value['name'] .'; type: '.$value['type']);
now it prints this:
"displaySlug: 1; type: boolean"
When I save this to DB, it also gets saved as number and not as boolean, it becomes 1.
$setting = new Setting();
$setting->key = $key;
$setting->type = $type;
$setting->value = $value;
$this->settings()->save($setting);
return true;
value is where boolean gets stored as 1, I'd like to store it as true even if its just a string in db
I would like it to be boolean in db. What can I do to get it boolean instead of number?
Column where I am saving it is a string column.
Upvotes: 0
Views: 3049
Reputation: 4925
PHP doesn't print boolean value as "true"
or "false"
but rather automatically converts it into "1"
or ""
(empty string).
MySQL BOOLEAN datatype itself doesn't save boolean as true
/false
because it is an alias of tinyint(1)
.
BOOL, BOOLEAN
These types are synonyms for TINYINT(1). A value of zero is considered false. Nonzero values are considered true
Reference: MySQL Manual
There is also a gotcha in saving boolean as string in the database that php treats the value "false"
as true
because it's a non empty string.
var_dump((boolean) "FALSE"); // bool(true)
var_dump((boolean) "TRUE"); // bool(true)
var_dump((boolean) ""); // bool(false)
Read PHP Types Comparison for more information.
So, a safer way of handling boolean in database would be to save it as integer value of 1
/0
in your database and use Attribute Casting for conversion.
Upvotes: 2
Reputation: 8297
When printing/dumping the boolean value in PHP, it gets cast to a string.
A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values.1
You could convert a string back to a boolean:
$value = (bool)$valueFromDatabase;
Bear in mind:
When converting to boolean, the following values are considered FALSE
Instead of converting to a bool explicitly, one could just use truthy/falsey coersion of strings (refer to PHP type comparison tables for more information).
$valueFromDatabase= 0; //false
if (!$valueFromDatabase) { //0 is falsey, negated yields truthy expression
//action to take server-side when value is false
}
$dataForJSONOutput = array('value' => $valueFromDatabase);
//use this in Javascript code - e.g. VueJS
echo '<script type="text/javascript">var values = '.json_encode($dataForJSONOutput).'; </script>;
See a 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
Upvotes: 2
Reputation: 3260
You cannot pass a true boolean in a string. What you will have to do in your PHP is convert it to actual true and false before writing to the database. However, if your database field is set as the boolean datatype, putting 0 or 1 should work just fine.
1/0 = True/False.
But you could test in php and say: if($var == "1" $var = true || if $var == "0" $var = false);
Then you can write $var
to the record. Obviously, you'll want to adapt that to your code structure but you should get the idea.
Upvotes: 1