На данный момент созданная мной роль работает правильно, но при добавлении чтения, создания, обновления и удаления возникают трудности. это то, что я ожидаю:
мои ожидания
Это моя таблица ролей:
таблица ролей
Это моя таблица user_role:
user_role
Это моя модель пользователя:
public function roles() {
// return $this->belongsToMany('App\Role');
//test
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');
}
Это мой контроллер пользователя
public function edit($id)
{
$id = Crypt::decrypt($id);
//Auth user
$authUser = Auth::user()->id;
$authAdmin = Auth::user()->is_admin;
if ($authUser == $id && $authAdmin == false) {
return redirect('/users')->with('warning', 'Kamu Tidak Dapat Mengubah Data Dirimu Sendiri.');
}
return view('layouts.user.edit')->with(['user' => User::with('roles')->find($id), 'roles' => Role::all()]);
// Test
// $user = User::with('roles')->find($id);
// return json_encode($user);
}
И это мое мнение:
<div class="form-group row">
<label for="role"
class="col-sm-2 col-form-label col-form-label-sm">Role</label>
<!-- Input text validasi -->
<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="col d-inline mr-3">
<input name="create" value="" id="customCheck111" class="custom-control-input" type="checkbox">
<label class="custom-control-label">Read</label>
</div>
<div class="col d-inline mr-3">
<input name="create" value="" id="customCheck111" class="custom-control-input" type="checkbox">
<label class="custom-control-label">Create</label>
</div>
<div class="col d-inline mr-3">
<input name="update" value="" id="customCheck222" class="custom-control-input" type="checkbox">
<label class="custom-control-label">Update</label>
</div>
<div class="col d-inline mr-3">
<input name="delete" value="" id="customCheck333" class="custom-control-input" type="checkbox">
<label class="custom-control-label">Delete</label>
</div>
{{-- End Testing --}}
</div>
</div>
</div>
@endforeach
@endif
</div>
Большое спасибо.