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

vadimsg's avatar

L5 Form checkbox returns always checked

Hello there, as some of us know this kind of a problem with checkboxes was/is still open. Currently I'm developing with L5 and Illuminate Html package and I'm getting this problem again. The interesting part is that I could fix this problem in L4 with some tweeks but now nothing works. It always returns as checked. Maybe someone has a clue what could be the problem or there is a solution that I don't know about?

Currently I'm trying this method of Form model binding. The code in my controller that makes an array key for the checkbox if it containts relationship.

// ItemController.php

if ($prop->items->contains($id))
    $data['props'][$prop->id] = $prop->id;

My form looks like this.

{!! Form::model($data, ['route' => ['admin.item.edit.post', $data['id']]]) !!}

And the checkbox like this.

{!! Form::checkbox('props['.$prop->id.']', $prop->id) !!}

As this relationship do exist and the correct array key is created the checkbox is checked but when I uncheck it and post data, the model gives me checked input.

0 likes
4 replies
devinfd's avatar

I seem to remember having this problem before when using form model binding. I believe I resolved it by making sure the value in the model that corresponds to the checkbox is a boolean.

jekinney's avatar

The function in FormBulder class

public function checkbox($name, $value = 1, $checked = null, $options = array())
    {
        return $this->checkable('checkbox', $name, $value, $checked, $options);
    }

Looks like you have to items you are passing in to your form checkbox. The third will set the html to checked if is is true or has a value (not false or null). Seems it may be skipping your first item which should be the name to pass into you input.

<label for="display_desc">
            {!! Form::checkbox('display_desc') !!}Display Category's Description
</label>

The above is a copy paste from something I have open at the moment which form model binding will set to true (checked) or not checked (false) by default. The only thing I am supplying is the name field for the form. I perform a check to see if is was checked by:

L4 
$display_desc = Input::has('display_desc')? 1 : 0; // or ? true : false

L5

$display_desc = $request->has('display_desc')? 1 : 0;

This sets a boolean value.

If you need a specific value like the ID to set a relationship:

 <label for="display_desc">
    {!! Form::checkbox('category[]', $category->id) !!} {{ $category->title }}
</label>

This should work in the same way in form model binding and a form open case but you are declaring the value.

edit:

{!! Form::checkbox('props['.$prop->id.']', $prop->id) !!}

You are setting all three because you have to use :

{!! Form::checkbox('props["$prop->id"]', $prop->id) !!}

no . (periods) and single ' (quote) inside your array value if your using ' (single quote) outside already. If you would have used developer tools to see the outputted HTML in a web browser I am sure you would have caught this! :).

vadimsg's avatar

Thanks @jekinney for such big answer! The last part you edited about using:

// This
{!! Form::checkbox('props["$prop->id"]', $prop->id) !!}

// Instead of
{!! Form::checkbox('props['.$prop->id.']', $prop->id) !!}

// Outputed html in chrome is
<input name="props["$prop->id"]" type="checkbox" value="1">

I tried as simple checkbox as it could get and still request has it.

// Controller
$data['test'] = true;

// View
{!! Form::checkbox('test', 1) !!}

I'm loosing it..

1 like
vadimsg's avatar

Ladies and Gentlemen please be careful when foreaching stuff. I was foreaching 2 times my checkboxes because I had bootstrap tabs. So yeah duplicated checkboxes do bug :)

1 like

Please or to participate in this conversation.