Creating a Checkbox Input Field

Problem

You want to create a checkbox field for your Blade template.

Solution

Use the Form::checkbox() method.

You're only required to use the first argument, name.

{{ Form::checkbox('agree') }}

This produces the following HTML.

<input name="agree" type="checkbox" value="1">

You can specify the value with a second argument.

{{ Form::checkbox('agree', 'yes') }}

Now the value, if checked, will be "yes".

<input name="agree" type="checkbox" value="yes">

If you want to default the value as checked, pass true as the third argument.

{{ Form::checkbox('agree', 1, true) }}

This adds the checked attribute.

<input checked="checked" name="agree" type="checkbox" value="1">

Finally, you can add additional attributes to the input field with the fourth argument.

{{ Form::checkbox('agree', 1, null, ['class' => 'field']) }}

Now the field has the class attribute.

<input class="field" name="agree" type="checkbox" value="1">

Discussion

The checkbox will automatically get checked based on any flash data.

If you redisplay the form because of errors, your checkbox will retain what the user had previously.

Also, if you've bound a model to the form, it will pull the value from the model's data. See Creating a New Model Based Form.

comments powered by Disqus