【问题标题】:Livewire Selected CheckboxLivewire 选中的复选框
【发布时间】:2020-10-27 08:07:05
【问题描述】:

我在尝试显示数据库中的权限列表然后尝试编辑角色及其权限时遇到问题,我无法让表单具有分配给要检查的角色的权限。

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 与列表数组相同,并使其 $permission = ['permissions.15'];或 $permission = [15] 等,但我无法选择分配给角色的权限。

任何帮助将不胜感激。 谢谢

【问题讨论】:

    标签: laravel laravel-livewire


    【解决方案1】:

    好的,我只需要让返回的数组使用 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 2021-03-10
      • 2021-03-01
      • 2022-10-25
      • 2022-01-20
      相关资源
      最近更新 更多