/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request) { $data = $request->all(); $caseFields = Schema::connection('mysql')->getColumnListing('rj_cases'); if (isset($data)) { $case = RjCase::find($data['id']); foreach ($data as $key => $value) { if (in_array($key, $caseFields)) { if ($this->utilityService->isDateField($key)) { $value = !empty($value) ? $this->utilityService->parseToMysqlDate($value) : null; } $case[$key] = $value; } } $case->save(); } if (isset($data['victims'])) { $victimFields = Schema::connection('mysql')->getColumnListing('victims'); foreach ($data['victims'] as $newVictim) { $victim = Victim::find($newVictim['id']); foreach ($newVictim as $key => $value) { if (in_array($key, $victimFields)) { if ($this->utilityService->isDateField($key)) { $value = $this->utilityService->parseToMysqlDate($value); } $victim[$key] = $value; } } $victim->save(); } } if (isset($data['offenders'])) { $offenderFields = Schema::connection('mysql')->getColumnListing('offenders'); foreach ($data['offenders'] as $newOffender) { $offender = Offender::find($newOffender['id']); foreach ($newOffender as $key => $value) { if (in_array($key, $offenderFields)) { if ($this->utilityService->isDateField($key)) { $value = $this->utilityService->parseToMysqlDate($value); } $offender[$key] = $value; } } $offender->save(); } } if (isset($data['charges'])) { $existingCharges = RjCase::find($data['id'])->charges; foreach ($data['charges'] as $newCharge) { if (!in_array($newCharge, $existingCharges->toArray())) { $case->charges()->detach(); } } foreach ($data['charges'] as $newCharge) { $case->charges()->attach($newCharge); } } if (isset($data['facilitators'])) { $existingFacilitators = RjCase::find($data['id'])->users; foreach ($data['facilitators'] as $newFacilitator) { if (!in_array($newFacilitator, $existingFacilitators->toArray())) { $case->users()->detach(); } } foreach ($data['facilitators'] as $newFacilitator) { $case->users()->attach($newFacilitator); } } return response()->json(array('id' => $case->id)); }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $data = $request->all(); $victimFields = Schema::connection('mysql')->getColumnListing('victims'); if (isset($data)) { $victim = Victim::find($data['id']); foreach ($data as $key => $value) { if (in_array($key, $victimFields)) { if ($this->utilityService->isDateField($key)) { $value = $this->utilityService->parseToMysqlDate($value); } $victim[$key] = $value; } } $victim->save(); if (isset($data['cases'])) { $existingCases = $this->victimService->getVictimById($data['id'])->rjCases; foreach ($data['cases'] as $newCase) { if (!in_array($newCase, $existingCases->toArray())) { $victim->rjCases()->detach(); } } foreach ($data['cases'] as $newCase) { $victim->rjCases()->attach($newCase); } } } return response()->json(array('success' => 'true')); }