public function getUpdateModule(Request $request) { $data = $request->all(); $i = 0; $role = Role_user::find($data['role_user']); if (isset($data['semua_module'])) { $module = Module::all(); foreach ($module as $value) { $upload[$i]['module_id'] = $value->id; $i++; } } else { foreach ($data as $key => $value) { if (substr($key, 0, 2) == 'x_') { $upload[$i]['module_id'] = substr($key, 2); $i++; } } } $role_1 = Role_user::where('id', $data['role_user'])->with('module_user')->firstOrFail()->toArray(); $array_user = $upload; $array_db = $role_1['module_user']; // dd($array_user); $i = 0; foreach ($array_user as $value) { $collection = collect($array_db); $bool = $collection->contains($value['module_id']); if ($bool == false) { $role->module_user()->attach($value['module_id'], ['created_by' => Auth::user()->id]); } $i++; } $i = 0; foreach ($array_db as $value) { $collection = collect($array_user); $bool = $collection->contains($value['id']); if ($bool == false) { $role->module_user()->detach($value['id']); } $i++; } // if(count($role_1['module_user']) > count($upload)){ // for ($i=0; $i<count($role_1['module_user']); $i++){ // if($role_1['module_user'][$i]['id'] != $upload[$i]['module_id'] ){ // $role->module_user()->detach($upload[$i]['module_id']); // } // } // // }elseif(count($role_1['module_user']) > count($upload)){ // echo 'lebih besar'; // } $role->module_user()->attach($upload, ['created_by' => Auth::user()->id]); return redirect('hrga/role-module'); }
/** * Rompe enlace entre un usuario y un rol * * @param int $role_id * @param int $user_id * @return Response */ public function detach($user_id, $role_id) { $data["user_id"] = $user_id; $data["role_id"] = $role_id; $validator = Validator::make($data, ['user_id' => 'integer', 'role_id' => 'integer']); if ($validator->fails()) { return response()->json(["msg" => "alert", "validator" => $validator->messages()], 200); } if ($user = User::find($user_id)) { if ($role = Role::find($role_id)) { if ($role_user = Role_user::where('role_id', $role_id)->where('user_id', $user_id)->delete()) { return response()->json(["msg" => "Success"], 200); } else { $description = "La relacion entre el usuario y el rol no existe"; } } else { $description = "No se ha encontrado el rol"; } } else { $description = "No se ha encontrado el usuario"; } return response()->json(["msg" => "error", "description" => $description], 200); }