Livewire 选中的复选框
Livewire Selected Checkbox
我在尝试显示数据库中的权限列表然后尝试编辑角色及其权限时遇到问题,我无法让表单将权限分配给要检查的角色。
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
权限列表数组:
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和list数组一样,也让$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
我在尝试显示数据库中的权限列表然后尝试编辑角色及其权限时遇到问题,我无法让表单将权限分配给要检查的角色。
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
权限列表数组:
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和list数组一样,也让$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