project_id; $subproyekId = $request->subproyek_id; $listMaterialId = $request->material_id; if (!$projectId && !$subproyekId) { return response()->json(['status' => 'failed', 'message' => 'Required proyek_id or subproyek id', 'code' => 400]); } if (is_array($listMaterialId) && count($listMaterialId) > 0) { $result = 0; MaterialProject::where('proyek_id', $projectId)->delete(); MaterialProject::where('proyek_id', $subproyekId)->delete(); foreach ($listMaterialId as $material_id) { $data = [ 'material_resource_id' => (int)$material_id ]; if ($projectId && $projectId != "" && (int)$projectId > 0) { $data['proyek_id'] = $projectId; } else if ($subproyekId && $subproyekId != "" && (int)$subproyekId > 0) { $data['subproyek_id'] = $subproyekId; } $insert = MaterialProject::create($data); if ($insert) { $result++; } else { $result--; } } if ($result > 0) { return response()->json(['status' => 'success', 'message' => 'Material Project successfull updated', 'code' => 200]); } else { return response()->json(['status' => 'failed', 'message' => 'Material Project failed updated', 'code' => 400]); } } else { MaterialProject::where('proyek_id', $projectId)->delete(); MaterialProject::where('proyek_id', $subproyekId)->delete(); return response()->json(['status' => 'success', 'message' => 'Material Project Successfull updated', 'code' => 200]); } } }