RushVan
RushVan

Reputation: 363

How do I data bind to a checkbox input Laravel?

Using Laravel 5.1

I have a form that I am data binding to. Everything work as expected except for the checkbox.

Here is my input (blade template);

<input type="checkbox" name="noContact" value="{{ $profile['noContact'] }}">

The field is a boolean and the checked value should be 1. Doesn't seem to be binding. What am I missing?

Thanks!

Upvotes: 1

Views: 1692

Answers (3)

Hongbin Wang
Hongbin Wang

Reputation: 1186

In laravel, you can try to use Forms & HTML.

Form::checkbox( "noContact", $profile['noContact']);

Upvotes: 0

soywod
soywod

Reputation: 4520

Maybe try to use checked="checked" instead of value attribute :

<input type="checkbox" name="noContact"{{ $profile['noContact'] ? ' checked="checked"' : '' }}>

Upvotes: 3

Claudio King
Claudio King

Reputation: 1616

In your example if you set the value to "bla bla" and the checkbox is checked, when you send the form in the controller you will get exactly "bla bla".

If you want only to check if the checkbox is checked or not, just set the value to 1 and in the controller handle it like this:

$checked = (bool)Input::get("checkbox_name");

Or in a condition:

if(!empty(Input::get("checkbox_name"))){...}

Upvotes: 2

Related Questions