Mamulasa Mamulasa - 2 months ago 30x
PHP Question

Laravel 5 Form Model Binding Checkbox Values

I am using a checkbox in a form, for some reasons I can't store the value of the checkbox status (checked or unchecked).

I am using form model binding.

My form is:

{!! Form::model($profile, ['method' => 'PATCH', 'action' => ['ProfilesController@update', $profile->id]]) !!}

<div class="form-group">
{!! Form::label('wifi', 'Wifi') !!}
{!! Form::checkbox('wifi','yes', $profile->wifi) !!}Wifi

{!! Form::close() !!}

My Schema:


But I also tried it with an integer

I can't figure out what I am doing wrong


Your this piece of code

{!! Form::checkbox('wifi','yes', $profile->wifi) !!}Wifi

is generating this

<input checked="checked" name="wifi" type="checkbox" value="yes">

Which means that you are sending the value yes to the server but your column data type is not varchar/text. You set it to boolean.

update your code to

{!! Form::checkbox('wifi', $profile->wifi, $profile->wifi) !!} Wifi

Also, include your wifi key in fillable and casts array. like so

protected $fillable = [ ..., 'wifi' ];

protected $casts = [ 'wifi' => 'boolean' ];

Note: your schema code


nullable is not a property, it is a function. so update it as well


After that refersh your database migration

php artisan migrate:refresh