如何在数据库中设置值时将复选框显示为选中状态 - Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25866027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show checkboxes as checked when values are set in the database - Laravel
提问by Railto
I'm setting up entrust and trying to manage roles via an edit users page. I have pulled the list of available roles as well as the roles that the user is already assigned to and have them passed to the view as $roles and $assignedRoles
我正在设置委托并尝试通过编辑用户页面管理角色。我已经提取了可用角色列表以及用户已经分配的角色,并将它们作为 $roles 和 $assignedRoles 传递给视图
Problem im having is getting the form to show the already assigned roles ($assignedRoles) as checked and the un-assigned roles (remainder of $roles )as not checked.
我遇到的问题是让表单显示已分配的角色 ($assignedRoles) 已选中,未分配的角色($roles 的其余部分)未选中。
$roles looks like
$roles 看起来像
[{"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"}]
and $userRoles looks like
和 $userRoles 看起来像
[{"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 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
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.
我的问题是我不知道如何制作它,以便在查看表单时它会显示已根据 $userRoles(上图)设置的角色。真的很感谢这方面的任何帮助。
回答by Nur Uddin
First you make an array with all data
首先你制作一个包含所有数据的数组
<?php
$all_data = array();
foreach($roles as $role){
$all_data[] = $role->id;
}
?>
Now you create checkbox
现在您创建复选框
@foreach ($roles as $role)
{{ Form::checkbox('role[]', $role->id, in_array($role->id, $all_data)) }}
{{ Form::label('role', $role->name) }}<br>
@endforeach
回答by Sanaulla
In Controller
在控制器中
$user = User::find($id);
$allRoles = Role::all();
$assignedRoles = $user->roles->pluck('id')->toArray();
In View
在视图中
<div class="form-group row" id="assign_hostel_fees">
<label for="inputEmail3" class="col-sm-3 control-label">Select Roles</label>
<div class="col-sm-9">
@foreach($allRoles as $role)
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="checkbox" name="roles[]" value="{{$role->id}}" {{in_array($role->id,$assignedRoles)?'checked':''}}>
{{$role->name}}
</label>
</div>
@endforeach
</div>
</div>
回答by Sougata Bose
this might help you -
这可能会帮助你 -
{{ Form::checkbox('agree', 1, true) }}
will generate -
将产生 -
<input checked="checked" name="agree" type="checkbox" value="1">
the third parameter defines if it will be checked or not. depending on the data(check the role id of $userRoles
with $roles
) generate a variable with true
or false
value.
第三个参数定义是否将被检查。根据数据(检查$userRoles
with的角色 ID $roles
)生成一个变量 withtrue
或false
value。