如何检索旧选中复选框的数组以在 laravel 中编辑 blade?

how to retrieve array of old selected checkbox to edit blade in laravel?

我遇到了一个错误,我想获取选中复选框的数组,它们是这样保存在数据库中的 -

,这个视图是这样的:

现在的问题是,我使用 role_id 作为访问它的 ID,这意味着当用户单击与 id = 1 关联的编辑按钮时表示管理员,权限设置为 role_id 1 应该在编辑页面上获取。编辑页面的ui是这样的:

,现在如何将所有选中的复选框检索回编辑选项并表示所有权限,它是 post ,评论,用户等。 如何在 blade?

上显示它们

这里是编辑的控制器:

  public function edit($role_id,Request $request)
    {
       
        $role_data = RolePermission::where(['role_id' => $role_id])->get();
      
        
        return view('AssignPermission.edit',compact('role_data'));
    }

这里是 blade 文件的视图??

    <td class="examplelink">
                              <input type="checkbox" name="Permissions[]" value="PostAdd"  @if (is_array(old('Permissions')) && in_array('PostAdd', old('Permissions'))) {{"checked='checked'"}}@endif>
                    </td>

如何检索所有数据到编辑表单???

编辑:这里是 dd($role_data->toArray()) 的结果

我像这样将值存储为静态:

        public function store(Request $request)
{
    $data = $request->all();
    $rules = array(
       'Roles' => 'required',
    );

    $validate=Validator::make($data,$rules);
    if ($validate->fails()) {
        return redirect()->back()->withInput()->withErrors($validate);
    }
    else{
       
         $data = $request->all();
        //  dd($data);   

        // Post CheckBox

        if(in_array('PostAdd', $request->get('Permissions'))){
            $PostAdd = new RolePermission;
            $PostAdd->role_id = $request->Roles;
            $PostAdd->Item = 'Post';
            $PostAdd->Permission = 'Add';
            $PostAdd->save();
        }
        if(in_array('PostView', $request->get('Permissions'))){
            $PostView = new RolePermission;
            $PostView->role_id = $request->Roles;
            $PostView->Item = 'Post';
            $PostView->Permission = 'View';
            $PostView->save();
        }
        
        if(in_array('PostEdit', $request->get('Permissions'))){
            $PostEdit = new RolePermission;
            $PostEdit->role_id = $request->Roles;
            $PostEdit->Item = 'Post';
            $PostEdit->Permission = 'Edit';
            $PostEdit->save();
        }

        if(in_array('PostDelete', $request->get('Permissions'))){
            $PostDelete = new RolePermission;
            $PostDelete->role_id = $request->Roles;
            $PostDelete->Item = 'Post';
            $PostDelete->Permission = 'Delete';
            $PostDelete->save();
        }

        // Comment checkBox
        if(in_array('CommentAdd', $request->get('Permissions'))){
            $CommentAdd = new RolePermission;
            $CommentAdd->role_id = $request->Roles;
            $CommentAdd->Item = 'Comment';
            $CommentAdd->Permission = 'Add';
            $CommentAdd->save();
        }
        if(in_array('CommentView', $request->get('Permissions'))){
            $CommentView = new RolePermission;
            $CommentView->role_id = $request->Roles;
            $CommentView->Item = 'Comment';
            $CommentView->Permission = 'View';
            $CommentView->save();
        }
        if(in_array('CommentEdit', $request->get('Permissions'))){
            $CommentEdit = new RolePermission;
            $CommentEdit->role_id = $request->Roles;
            $CommentEdit->Item = 'Commment';
            $CommentEdit->Permission = 'Edit';
            $CommentEdit->save();
        }
        if(in_array('CommentDelete', $request->get('Permissions'))){
            $CommentDelete = new RolePermission;
            $CommentDelete->role_id = $request->Roles;
            $CommentDelete->Item = 'Comment';
            $CommentDelete->Permission = 'Delete';
            $CommentDelete->save();
        }


        // User CheckBox
        if(in_array('UserAdd', $request->get('Permissions'))){
            $UserAdd = new RolePermission;
            $UserAdd->role_id = $request->Roles;
            $UserAdd->Item = 'User';
            $UserAdd->Permission = 'Add';
            $UserAdd->save();
        }
        if(in_array('UserView', $request->get('Permissions'))){
            $UserView = new RolePermission;
            $UserView->role_id = $request->Roles;
            $UserView->Item = 'User';
            $UserView->Permission = 'View';
            $UserView->save();
        }
        if(in_array('UserEdit', $request->get('Permissions'))){
            $UserEdit = new RolePermission;
            $UserEdit->role_id = $request->Roles;
            $UserEdit->Item = 'User';
            $UserEdit->Permission = 'Edit';
            $UserEdit->save();
        }
        if(in_array('UserDelete', $request->get('Permissions'))){
            $UserDelete = new RolePermission;
            $UserDelete->role_id = $request->Roles;
            $UserDelete->Item = 'User';
            $UserDelete->Permission = 'Delete';
            $UserDelete->save();
        }
            
            $Message = "successfully added";
            return redirect('/Admin/AssignPermission')->with('success',$Message);
        }   
    }

一定要告诉我吗??这种方法对不对??建议其他存储和检索数据的方法 ???

更新代码: 这是编辑的功能:

    public function edit($role_id,Request $request)
    {
       
        $role_data = RolePermission::where(['role_id' => $role_id])->get()->map(function ($role){

            return $role->Item.$role->Permission;
        });;
        
        return view('AssignPermission.edit',compact('role_data'));
    }

这里是 blade 的更新代码:

                            <input type="checkbox" name="Permissions[]" value="PostAdd" {{is_array(old('Permissions',$role_data??[])) && in_array('PostAdd',old('Permissions',$role_data??[]))?'checked':null}} />

您可以执行以下操作

 <input type="checkbox" name="Permissions[]" value="PostAdd" {{is_array(old('Permissions',$role_data??[]))&&in_array('PostAdd',old('Permissions',$role_data??[]))?'checked':null}} />

在控制器中

   $role_data = RolePermission::where(['role_id' => $role_id])->get()->map(function ($role){

        return $role->Item.$role->Permission;
    })->toArray();