The third parameter of Form::checkbox()
is what indicates if the checkbox is checked or not. The second parameter is the value. So you can use something like this:
{{ Form::checkbox('isAdmin', '1', Input::old('isAdmin'), $roles->isAdmin)) }}
Where 'isAdmin' is the name of the input element and $roles->isAdmin
would be however you are getting the role/permission.
This way if isAdmin
is false the checkbox would appear as not checked. Just keep in mind that unchecked checkboxes will not be sent with the form input.
Thanks, not sure how I can use this in a foreach loop tho.
I am iterating through all roles in $roles to produce the check boxes bit need it to automatically check any that are also contained in $userRoles
Can we see an example of the data that will be stored in the arrays?
Sure, should have provided that already, doh!
$roles has the following array:
[{"id":1,"name":"Admin","created_at":"2014-09-15 14:26:24","updated_at":"2014-09-15 14:26:24"},{"id":2,"name":"Pastor","created_at":"2014-09-15 14:26:34","updated_at":"2014-09-15 14:26:34"},{"id":3,"name":"Elder","created_at":"2014-09-15 14:26:43","updated_at":"2014-09-15 14:26:43"},{"id":4,"name":"Ministry Leader","created_at":"2014-09-15 14:26:55","updated_at":"2014-09-15 14:26:55"}]
$userRoles has the following array:
[{"id":1,"name":"Admin","created_at":"2014-09-15 14:26:24","updated_at":"2014-09-15 14:26:24","pivot":{"user_id":1,"role_id":1}}]
I am currently iterating through the available roles in the view using
@foreach ($roles as $role)
{{ Form::checkbox('role[]', $role->id) }}
{{ Form::label('role', $role->name) }}<br>
@endforeach
but my problem is that I do not know how I can make it so that when viewing the form it will show which roles are already set as per $userRoles (above).
Really appreciate any help on this.
Ok, all sorted now.
Had to iterate through the $userRoles array to pull out just the ID for each item then in the view changed the Form::checkbox to:
{{ Form::checkbox('role[]', $role->id, in_array($role->id, $selectedRoles)) }}
and how can i remove a record if I un-check the check box from the edit view?
@JeyKeu: I just found the solution here: http://nielson.io/2014/02/handling-checkbox-input-in-laravel/
The little trick is to add an hidden field BEFORE the checkbox with the same name:
{{ Form::hidden('approved', false) }}
{{ Form::checkbox('approved', true) }}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community