我有一块模板:
@foreach ( Permission::all() as $permission )
{{ Form::checkbox('permissions[]', $permission->id, $role->permissions->contains($permission->id) ) }} <label>{{ $permission->label }}</label>
{{ var_dump($role->permissions->contains($permission->id)) }}
@endforeach
这会输出以下html:
<input checked="checked" name="permissions[]" type="checkbox" value="1"> <label>View entity</label>
<small>boolean</small> true
<input checked="checked" name="permissions[]" type="checkbox" value="3"> <label>Edit Entity</label>
<small>boolean</small> true
<input checked="checked" name="permissions[]" type="checkbox" value="4"> <label>Delete entity</label>
<small>boolean</small> false
W T H正在进行中。任何想法?
修改
更加困惑。如果我将false
传递给Form :: checkbox函数。仅在没有checked参数的情况下呈现第一个项目。我放弃了:(
答案 0 :(得分:0)
发现'问题'。在github上查看此问题:https://github.com/laravel/framework/issues/5078:
我正在解析的模型有permissions()数组。 false将被忽略,复选框将显示为已选中。即使$ permission-&gt; id与角色无关。
FormBuilder @ getCheckboxCheckedState
return is_array($posted) ? in_array($value, $posted) : (bool) $posted;
即使设置了$ permission-&gt; id,此行也会返回true。或者给出“检查状态”参数。
Perosnally我认为所有那些isChecked检查只应在$ checked is_null时执行。
FormBuilder @辨认的()
$checked = $this->getCheckedState($type, $name, $value, $checked);
应该是
if ( is_null($checked) ) $checked = $this->getCheckedState($type, $name, $value, $checked);