Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincej's avatar
Level 15

Blade Checkbox returns null when unchecked

I don't get it. My checkbox delivers true to the DB when checked. But if I uncheck it, I get a null. So what am I doing wrong?

 <div class="col">
<input type="checkbox" name="staff" value="true"  @if($child->staff == 'true') checked @endif >
 </div>

0 likes
11 replies
Nakov's avatar

You are not doing anything wrong, that's normal behavior for a checkbox.. You should be checking in your controller if the request has value for that input..

$request->has('staff') instead of ->input for a checkbox.

vincej's avatar
Level 15

Ok, thank you for your rapid reply. When I have a checked checkbox I get a true result. However, with an empty checkbox I get nothing. Not even a request->staff.

So - don't tell me, all I need to do is set the default in the table to false, and then when the box is check it goes to true.

Is that it?? All day I had the belief that my checkbox would send a false AND a true. Please confirm if that's it.

I rarely use checkboxes. I feel quite the chump. :o)

Nakov's avatar

As i said above use this

$request->has('staff')

it will always work. Having it just defaut as false and then changing it to true it won't work if you want to go back to false with an update, with what I am giving you to use, it will always work.

vincej's avatar
Level 15

I don't understand your point, nor what you are suggesting. Here is my controller:

public function upDateChildDetails(Request $request){
         child::where('child_id', $request->child_id)
         ->update([
             'childFirstName' => $request->childFirstName,
             'childLastName' => $request->childLastName,
             'grade' => $request->grade,
             'staff' => $request->staff,
             'parentFirstName' => $request->parentFirstName,
             'parentLastName' => $request->parentLastName,
             'parentOnePhone' => $request->parentOnePhone,
             'parentTwoFirstName' => $request->parentTwoFirstName,
             'parentTwoLastName' => $request->parentTwoLastName,
             'parentTwoPhone' => $request->parentTwoPhone,
             'guardianFirstName' => $request->guardianFirstName,
             'guardianLastName' => $request->guardianLastName,
             'guardianPhone' => $request->guardianPhone,
             'status'=> $request->status,
             'notes'=>$request->notes,

             ]);

Nakov's avatar
Nakov
Best Answer
Level 73

Here, change this line

'staff' => $request->staff,

to this

'staff' => $request->has('staff'),
vincej's avatar
Level 15

OUSTANDING ! It is working. You are a rock star. I did have to change this to 1 as false and true no longer apply:

@if($child->staff == '1')

I had completely misunderstood how checkboxes work as I so rarely ruse them Additionally I was wholly unfamiliar with the ->has() method. So I have learned a couple of thing tonight. Thank you. I guess I will just convert my table to int rather than varchar.

best response award.

jlrdw's avatar

@vincej in the database a checkbox field is tinyint. The MySQL manual explains all of this.

1 like
vincej's avatar
Level 15

@jlrdw Yup, I meant tinyint, ad I used that. I have been hunting around the docs for this request->has(' ') function. Need to spend more time reading up on requests. These checkboxes have been freaking me out for a while. I seldom use them. Anyway, all good now. Cheers.

MichalOravec's avatar

@vincej I would use ternary operator instead of if in checkbox.

<input type="checkbox" name="staff" value="1" {{ $child->staff ? 'checked' : '' }}>
newbie360's avatar

an other use case

<input type="hidden" name="staff" value="0">
<input type="checkbox" name="staff" value="1"  @if($child->staff) checked @endif >

Form Request

public function rules()
{
    return [
        'staff' => 'required|boolean',
    ];
}

Model

protected $casts = [
    'staff' => 'boolean',
];

Please or to participate in this conversation.