0
votes

I'm sending data with FormData object to my laravel backend, issue is I have a checkbox input which is causing me trouble, my backend logs the following error message:

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'true' for column 'isVisible' ...

In my migrations isVisible field is declared as a boolean:

$table->boolean('isVisible')->default(false);

If I hack into the payload and send an 1 instead of 'true' or 0 instead of 'false' my Post entry is created succesfully.

Any idea how I can solve this?

2
Send value as 1 or 0 or change it in controller before saving to DB. - ascsoftw
Or use TRUE/FALSE without quotes. By quoting the values, you're sending them as strings as boolean values. - aynber
Please take a look at your database table. The datatype for isVisible should be tinyint with the length 1 and default value 0. Now you can store the values either by using TRUE or FALSE or even 1 or 0 WITHOUT QUOTING. - Assad Ullah Ch

2 Answers

0
votes

Send this before database

$isVisible === true || strtolower($isVisible) == 'true'
0
votes

Laravel has a nice solution for this:

$model->is_visible = $request->has('isVisible');

(if the checkbox is not checked, it will not be present in the $request bag, so the 'has' function will output false. If checked, it returns true (as boolean).