如何解决Livewire选定复选框
在尝试显示数据库中的权限列表,然后尝试编辑角色及其权限时遇到问题,我无法获得将权限分配给要检查的角色的表单。
PHP:
public $role;
public $permissions
public $permissionList;
public function render(PermissionService $permissionService)
{
$this->permissionList = $permissionService->getPermissions();
$this->permissions = $permissionService->getRolePermissions($this->role);
return view('livewire.roles.edit');
}
查看:
@foreach($permissionList as $key => $permission)
<div class="form-row">
<div class="form-group col-md-6 h5">
{{ ucfirst($key) }}
</div>
<div class="form-group col-md-6">
@foreach($permission as $item)
<div>
<input type="checkBox" id="{{ $item['id'] }}" name="{{ $item['id'] }}" value="{{ $item['id'] }}" wire:model="permissions.{{ $item['id'] }}"/> {{ ucfirst($item['name']) }}
</div>
@endforeach
</div>
<hr/>
</div>
@endforeach
PermissionList数组:
array:4 [▼
"administration" => array:1 [▼
0 => array:2 [▼
"id" => 15
"name" => "preferences"
]
]
"users" => array:5 [▼
1 => array:2 [▼
"id" => 16
"name" => "view"
]
2 => array:2 [▼
"id" => 17
"name" => "create"
]
3 => array:2 [▼
"id" => 18
"name" => "edit"
]
4 => array:2 [▼
"id" => 19
"name" => "delete"
]
5 => array:2 [▼
"id" => 20
"name" => "impersonate"
]
]
我使权限列表数组具有一个类别,并将权限分配给该类别,这纯粹是出于显示目的。
我试图使$ permissions与列表数组相同,也将其设为$ permission = ['permissions.15'];或$ permission = [15]等,但我无法选择分配给该角色的权限。
任何帮助将不胜感激。 谢谢
解决方法
好吧,我只需要使返回的数组使用id作为索引,然后就不需要对被检查的对象进行foreach了,一切都会按预期进行。
array:4 [▼
"administration" => array:1 [▼
15 => array:2 [▼
"name" => "preferences"
]
]
"users" => array:5 [▼
16 => array:2 [▼
"name" => "view"
]
17 => array:2 [▼
"name" => "create"
]
18 => array:2 [▼
"name" => "edit"
]
19 => array:2 [▼
"name" => "delete"
]
]
]
查看:
@foreach($permissionList as $key => $permission)
<div class="form-row">
<div class="form-group col-md-6 h5">
{{ ucfirst($key) }}
</div>
<div class="form-group col-md-6">
@foreach($permission as $index => $item)
<div>
<input type="checkbox" id="{{ $index }}" name="{{ $index }}" value="1" wire:model="permissions.{{ $key }}.{{ $index }}"/> {{ ucfirst($item['name']) }}
</div>
@endforeach
</div>
<hr/>
</div>
@endforeach
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。