简体   繁体   中英

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

This is my roles table: roles table

This is my user_role table: user_role table

My json response after click button Edit: json response

This is my User 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:

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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