简体   繁体   English

如何在 laravel 5.8 中更新此角色

[英]How to update this role in laravel 5.8

I want to give the roles along with the details of those roles, such as read, create, update, and delete.我想提供角色以及这些角色的详细信息,例如读取、创建、更新和删除。 How to change the role data with the following criteria.如何使用以下条件更改角色数据。

This is my expectations: role img这是我的期望:角色 img

This is my roles table: roles table这是我的角色表:角色表

This is my user_role table: user_role table这是我的 user_role 表: user_role 表

My json response after click button Edit: json response单击按钮后我的 json 响应编辑: json 响应

This is my User Model:这是我的用户 Model:

public function roles() {
  return $this->belongsToMany('App\Role')->withPivot([
    'rd',
    'cr',
    'up',
    'dl'
  ]);
}

public function hasAnyRoles($roles) {
 return null !== $this->roles()->whereIn('kd_role', $roles)->first();
}

public function hasAnyRole($role) {
  return null !== $this->roles()->where('kd_role', $role)->first();
}

public function karyawan() {
  return $this->hasOne('App\Karyawan', 'nik', 'nik');
}

public function roleUser() {
  return $this->hasMany('App\RoleUser');
}

This is my User Update Controller:这是我的用户更新 Controller:

public function update(Request $request, $id) {
  $user = User::find($id);

  for($i = 0; $i < count($request->roleUser); $i++) {
        $roleUser[] = [
            'role_id'   => $request->roleUser[$i],
            'rd'        => $request->rd[$i],
            'cr'        => $request->cr[$i],
            'up'        => $request->up[$i],
            'dl'        => $request->dl[$i],
            'user_ins'  => auth()->user()->nik,
        ];
    }

    RoleUser::insert($roleUser);

    $user->roles()->sync($request->roleUser);

    // return  json_encode($roleUser);
    return redirect('/users')->with('status', 'Data Berhasil Diperbarui!');
}

And this is my view:这是我的观点:

<div class="col-sm-10">
    @if ($roles->count())
    @foreach($roles as $role)
    <div class="custom-control custom-checkbox mb-3">
        <div class="row">
            <div class="col-sm-2">
                <input name="roleUser[]" value="{{$role->id}}" {{ $user->hasAnyRole($role->kd_role)?'checked':'' }} id="customCheck{{$role->id}}" class="custom-control-input" type="checkbox">
                <label class="custom-control-label" for="customCheck{{$role->id}}">{{$role->nm_role}}</label>
            </div>
            <div class="col-sm-10">
                {{-- Testing --}}
                <div class="custom-control custom-checkbox col d-inline mr-3">
                    <input value="true" name="rd" class="custom-control-input" {{ ($role['status']->rd == true)?'checked':'' }} id="customCheckR{{$role->id}}" type="checkbox">
                    <label class="custom-control-label" for="customCheckR{{$role->id}}">Read</label>
                </div>
                <div class="custom-control custom-checkbox col d-inline mr-3">
                    <input value="true" name="cr" class="custom-control-input" {{ ($role['status']->cr == true)?'checked':'' }} id="customCheckC{{$role->id}}" type="checkbox">
                    <label class="custom-control-label" for="customCheckC{{$role->id}}">Create</label>
                </div>
                <div class="custom-control custom-checkbox col d-inline mr-3">
                    <input value="true" name="up" class="custom-control-input" {{ ($role['status']->up == true)?'checked':'' }} id="customCheckU{{$role->id}}" type="checkbox">
                    <label class="custom-control-label" for="customCheckU{{$role->id}}">Update</label>
                </div>
                <div class="custom-control custom-checkbox col d-inline mr-3">
                    <input value="true" name="dl" class="custom-control-input" {{ ($role['status']->dl == true)?'checked':'' }} id="customCheckD{{$role->id}}" type="checkbox">
                    <label class="custom-control-label" for="customCheckD{{$role->id}}">Delete</label>
                </div>
                {{-- End Testing --}}
            </div>
        </div>
    </div>
    @endforeach
    @endif
</div>

Thank you.谢谢你。

If $request->roleUser is role ID then your code should be:如果$request->roleUserrole ID ,那么您的代码应该是:

public function update(Request $request, $id) {
    $userRoles= [];
    for($i = 0; $i < count($request->roleUser); $i++) {
        $userRoles[$request->roleUser[$i]] = [
            'rd'        => $request->rd[$i],
            'cr'        => $request->cr[$i],
            'up'        => $request->up[$i],
            'dl'        => $request->dl[$i],
            'user_ins'  => auth()->user()->nik,
        ];
    }

    $user->roles()->sync($userRoles);      
}

Untested code.未经测试的代码。 Please don't use in production without testing.未经测试,请勿在生产中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM