Hello, I have an adminController as below. Even though I checked the status box, it comes to the database as zero. I can’t figure out why if/else block doesn’t work. If I use debug i can see "on" value for checkbox
public function addPost(Request $request){
$name=$request->title;
$content=$request->content;
$status=$request->status;
if(!is_null($status)){
$status=0;
}
else{
$status=1;
}
Posts::create([
'name' =>$name,
'content' =>$content,
'status' =>$status
]);
}
addPost.php (it’s my form)
<div class="form-group">
<label for="">
<input type="checkbox" name="status" id="status">Status
</label>
</div>
3
Answers
This may help you a little bit change:
As I can see you are checking the !is_null on the code, because of which you are getting the 0 if you are checking the checkbox, the condition should be like this:
if you are still confused about this then pass a value from the checkbox and check that value in the controller:
Now, you can check like this in the controller: