public function saveReferees(Season $season, Competition $competition, Request $request) { $data = $request->all(); $competition->referees()->wherePivot('season_id', '=', $season->id)->detach(); $pairs = $this->getPairs($season, $competition); foreach ($pairs as $pair) { $pair->active = 0; $pair->save(); } $response = ['refs' => array(), 'crefs' => array()]; $country = Country::find($data['country']); $refs = json_decode($data['referees'], true); foreach ($refs as $ref) { $newRef = $competition->storeReferee($ref, $season, 0, $country); if (array_key_exists('styled', $ref)) { if ($ref['styled'] == 'new') { $newRef['location'] = Location::find($newRef->location_id)->name; array_push($response['refs'], $newRef); } } } $crefs = json_decode($data['court_referees'], true); foreach ($crefs as $cref) { $newRef = $competition->storeReferee($cref, $season, 1, $country); if (array_key_exists('styled', $cref)) { if ($cref['styled'] == 'new') { $newRef['location'] = Location::find($newRef->location_id)->name; array_push($response['crefs'], $newRef); } } } $response['pairs'] = $this->getPairs($season, $competition)->toArray(); return $response; }