Laravel | Checkbox state on edit page

nclsvh picture nclsvh · Mar 31, 2016 · Viewed 8.4k times · Source

I have a create page for a new Case in Laravel 5, where I can link services to the case. The services are checkboxes in my createCaseForm. The services are stored in the DB so I write to a ManyToMany table to have many Cases which have many services.

Example: When I create case1, I check the boxes for service1 and service3. So now the manyToMany table is filled in correctly. Now when I go to the edit page for the case I want the checkbox1 and checkbox3 to be checked by default, because they are already linked in the manyToMany table.

I know you can use something like Input::old('username'); for textfields, but I don't know how to do this for a checkbox.

I do use LaravelCollective most of the time, but for these checkboxes I didn't. So preferably I would like a HTML solution, IF POSSIBLE ofcourse.

Thanks

Answer

Qazi picture Qazi · Mar 31, 2016

This is sample code, you can do this, If you are using Blade template, then do this, 3rd parameter is true/false to checked/un-checked the checkbox

{!! Form::checkbox('agree', 1, ($model->checkbox ==1?true:null), ['class' => 'field']) !!}

here you are passing your model DB stored value to checkbox (in 3rd param)