/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $data = $request->all(); $victimData = isset($data['victim'][0]) ? $data['victim'][0] : null; $victimFields = Schema::connection('mysql')->getColumnListing('victims'); if (isset($victimData)) { $victim = new Victim(); foreach ($victimData as $field) { if (in_array($field['name'], $victimFields)) { if ($this->utilityService->isDateField($field['name'])) { $field['value'] = $this->utilityService->parseToMysqlDate($field['value']); } $victim[$field['name']] = $field['value']; } } $victim->save(); if (isset($data['cases'])) { foreach ($data['cases'] as $newCase) { $victim->rjCases()->attach($newCase); } } } return response()->json(array('success' => 'true', 'id' => $victim->id)); }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $data = $request->all(); $caseFields = Schema::connection('mysql')->getColumnListing('rj_cases'); if (isset($data['case'][0])) { $case = new RjCase(); foreach ($data['case'][0] as $field) { if (in_array($field['name'], $caseFields)) { if ($this->utilityService->isDateField($field['name'])) { $field['value'] = $this->utilityService->parseToMysqlDate($field['value']); } $case[$field['name']] = $field['value']; } } $case->save(); } if (isset($data['victim'])) { $victimFields = Schema::connection('mysql')->getColumnListing('victims'); foreach ($data['victim'] as $newVictim) { $victim = new Victim(); foreach ($newVictim as $key => $value) { if (in_array($key, $victimFields)) { if ($this->utilityService->isDateField($key)) { $value['value'] = $this->utilityService->parseToMysqlDate($value['value']); } $victim[$key] = $value['value']; } } $victim->save(); $case->victims()->attach($victim->id); } } if (isset($data['offender'])) { $offenderFields = Schema::connection('mysql')->getColumnListing('offenders'); foreach ($data['offender'] as $newOffender) { $offender = new Offender(); foreach ($newOffender as $key => $value) { if (in_array($key, $offenderFields)) { if ($this->utilityService->isDateField($key)) { $value['value'] = $this->utilityService->parseToMysqlDate($value['value']); } $offender[$key] = $value['value']; } } $offender->save(); $case->offenders()->attach($offender->id); } } if (isset($data['charge'])) { foreach ($data['charge'] as $newCharge) { $case->charges()->attach($newCharge); } } if (isset($data['facilitator'])) { foreach ($data['facilitator'] as $newFacilitator) { $case->users()->attach($newFacilitator); } } return array('status' => 'success', 'id' => $case->id); }