I have designed the checkbox like this, If checkbox is checked then the input value will be 1 else it’ll be 0
I have added logic with javascript
<script>
$('#exams, #materials, #notes, #course_live,, #schedule').on(
'change',
function() {
this.value = this.checked ? '1' : '0';
}).change();
</script>
Now when I submit the form if the checkbox is checked it’s working perfect, but if the checkbox is not checked it showing error:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'exams' cannot be null
But I want this that if the checkbox is not checked then the value should be 0, And it’s showing perfectly in Inspect, I have removed the validation from laravel but showing the same. Please help me out.
>Solution :
3 Ways :
1st: Conditional set in PHP
empty($request->input('your_input')) ? 0 : 1 ;
2nd: Set Database default to 0 (DB will convert null values to 0 automatically)
3rd: HTML trick: as Marwelln answer: you can put a hidden input before your checkbox with a default value.
<input type='hidden' name='foobar' value='0' /> <!-- default value if foobar checkbox is not checked -->
