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'); }