Support the ongoing development of Laravel.io →
Database Blade Forms
Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

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

Last updated 1 year ago.
0

Can we see an example of the data that will be stored in the arrays?

Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0
Solution

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)) }}
Last updated 1 year ago.
0

and how can i remove a record if I un-check the check box from the edit view?

0

@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) }}
Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

railto railto Joined 17 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.