微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何插入一个值并从禁用的文本和几个复选框中获取数据并存储在 Laravel 的数据库中?

如何解决如何插入一个值并从禁用的文本和几个复选框中获取数据并存储在 Laravel 的数据库中?

我遇到了错误。 所以事情是这样的。我有列名 role_permission,我必须在其中存储每个角色的角色,这是表单的 UI -

enter image description here

正如您在这里看到的,我选择了一些选项,用户可以在其中选择角色,例如 -admin、users、editor 和三个禁用的文本框,这些文本框是其权限将在数据库中设置的项目,以及用户可以选择的复选框权限。 现在主要的是,当用户提交按钮时,数据应该像这样保存

  id    roll_id   item    permission 
  1        1      post     add
  2        1      post     view
  3        1      comment   add

因为,如果用户选择了添加、编辑、查看帖子(项目)的权限,个人数据应该像我上面显示的那样,而不是数组或其他东西..每个权限都应该单独保存,指向它的项。

这里是迁移文件

public function up() {
    Schema::create('role_permission',function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('role_id');
        $table->string('Item');
        $table->string('Permission');
        $table->foreign('role_id')->references('id')->on('roles');
        $table->timestamps();
    });
}

这是它的形式:

<form method="post" action="{{ route('Admin.AssignPermission.save')}}" enctype="multipart/form-data">
    @csrf
    @if ($errors->any())
        <div class="alert alert-danger alert-dismissable">
              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
              <ul>
                  @foreach ($errors->all() as $error)
                  <li>{{ $error }}</li>
                  @endforeach
              </ul>
          </div>
        @endif
        
        <div class="form-group">
            <label> Assign Roles</label>
            <select style="margin-left: 10px;" class="form-select" aria-label="Default Select Role" name="Roles">
                    <option selected disabled>Roles</option>
                    @foreach ($roles as $role)
                        <option value="{{$role->id}}">{{$role->name}}</option>
                    @endforeach 
                </select>
        </div>
        <table class="table table-striped">
            <thead>
              <tr>
                <th scope="col">#</th>
                <th scope="col">Add</th>
                <th scope="col">View</th>
                <th scope="col">Edit</th>
                <th scope="col">Delete</th>
              </tr>
            </thead>
            <tbody>

                <!-- <input name="textBox1" id="textBox1" type="text" />
                <input name="buttonExecute" onclick="execute(document.getElementById('textBox1').value)" type="button" value="Execute" /> -->
                <tr id="post">
                    <th class="col-md-6">
                        <input class="form-control" type="text" name="Items[]"  value="Post" readonly>
                        <input type="hidden" name="selected[]" value="post">
                    </th>
                    <td>
                        <div class="form-check">
                            <input class="form-check-input" type="checkBox" value="Add" name="Permissions[]">
                        </div>
                    </td>
                    <td>
                        <div class="form-check">
                            <input class="form-check-input" type="checkBox" value="View" name="Permissions[]">
                        </div>
                    </td>
                    <td>
                        <div class="form-check">
                            <input class="form-check-input" type="checkBox" value="Edit" name="Permissions[]">
                        </div>
                    </td>
                    <td>
                        <div class="form-check">
                            <input class="form-check-input" type="checkBox" value="Delete" name="Permissions[]">
                        </div>
                    </td>
                </tr>
              <tr>
                <th class="col-md-6" id="Comments" >
                    <input class="form-control" type="text" name="Items[]"  value="Comments" readonly>
                </th>
               <td>
                    <div class="form-check">
                        <input class="form-check-input" type="checkBox" value="Add" name="Permissions[]">
                    </div>
                </td>
                <td>
                    <div class="form-check">
                        <input class="form-check-input" type="checkBox" value="View" name="Permissions[]">
                    </div>
                </td>
                <td>
                <div class="form-check">
                    <input class="form-check-input" type="checkBox" value="Edit" name="Permissions[]">
                </div>
            </td>
            <td>
                <div class="form-check">
                    <input class="form-check-input" type="checkBox" value="Delete" name="Permissions[]">
                </div>
            </td> 
              </tr>
              <tr>
                <th class="col-md-6" id="Users">
                    <input class="form-control" type="text" name="Items[]"  value="User" readonly>
                        
                </th>
                <td>
                    <div class="form-check">
                        <input class="form-check-input" type="checkBox" value="Add" name="Permissions[]">
                    </div>
                </td>
                <td>
                    <div class="form-check">
                        <input class="form-check-input" type="checkBox" value="View" name="Permissions[]">
                    </div>
                </td>
                <td>
                    <div class="form-check">
                        <input class="form-check-input" type="checkBox" value="Edit" name="Permissions[]">
                    </div>
                </td>
                <td>
                    <div class="form-check">
                        <input class="form-check-input" type="checkBox" value="Delete" name="Permissions[]">
                    </div>
                </td>
              </tr>
            </tbody>
          </table>  

          <div style="margin-top:5px; margin-left:1030px;">
              <input type="submit" class="btn btn-primary" value="Submit">
          </div>
    </form>

这是它的存储方法

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

    $validate=Validator::make($data,$rules);
    
    if ($validate->fails()) {
        return redirect()->back()->withinput()->withErrors($validate);
    } else {
       foreach($request->Items as $key=>$Items){
           foreach ($request->Permissions as $key => $Permissions) {
                $form_data = array(
                    'role_id'    => $data['Roles'],'Item'       => $data['Items'],'Permission' => $data['Permissions'],);
           }
       }
       $RolePermission = RolePermission::create($form_data);
       $Message = "successfully added";
       return redirect('/Admin/users')->with('success',$Message);
    }   
}

,使用这个方法却报错???或如何做到这一点任何其他方法都会有所帮助 ???

这是错误所在。

enter image description here

这是我的表格结构:

enter image description here

解决方法

你让事情变得比需要的更复杂。首先将您的视图更改为与此类似。

// name field having [post] for post
<input class="form-check-input" type="checkbox" value="Delete" name="Permissions[post]">
// name field having [comment] for comment
<input class="form-check-input" type="checkbox" value="Delete" name="Permissions[comment]">

通过表单提交时,上面的代码会导致这样的结果

dd($request->all()); 
// below is output of this
[
   "Permissions" => [
      "post" => ["Add","Delete"],"comment" => ["Delete"]
   ]
]

然后在控制器中,您只需对这些进行排序并根据需要执行操作

  // Looping though permissions
  foreach ($request->Permissions as $itemName => $permissions) {
      // $permissions will be an array ["Add","Delete"]
      // $itemName will "post"
      // We loop again in permissions to output single data permission
      foreach($permissions as $permission) {
          $form_data = [
             'role_id'    => $data['Roles'],'Item'       => $itemName,// post
             'Permission' => $permission,// Add
          ];

          // Perform action here as you suit. Create/Delete based on requirement
          if(!RolePermission::where($form_data)->exists()){
              RolePermission::create($form_data);
          }
      }
 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。