public function store(Request $request) { $input = $request->all(); $predictions = []; foreach ($input['home_score'] as $key => $score) { $predictions[$key]['home_score'] = $score; } foreach ($input['away_score'] as $key => $score) { $predictions[$key]['away_score'] = $score; } foreach ($input['fix_id'] as $key => $id) { $predictions[$key]['fixture_id'] = $id; $predictions[$key]['user_id'] = $this->user->id; } foreach ($predictions as $prediction) { $fxt = \App\Fixture::findOrFail($prediction['fixture_id']); if ($fxt->isClosed()) { continue; } $pred = \App\Prediction::firstOrNew(['user_id' => $prediction['user_id'], 'fixture_id' => $prediction['fixture_id']]); $pred->home_score = $prediction['home_score']; $pred->away_score = $prediction['away_score']; if ($pred->valid()) { $user = \App\User::find($prediction['user_id']); $gameweek = $pred->fixture->gameweek; $exists = $user->gameweeks->contains($gameweek->id); if (!$exists) { $user->gameweeks()->attach($gameweek->id, ['score' => 0, 'rank' => null, 'boost_id' => null]); } if (isset($input['boost_id']) and $pred->fixture_id == $input['boost_id']) { $user->gameweeks()->updateExistingPivot($gameweek->id, ['boost_id' => $input['boost_id']], false); } $pred->save(); } } return redirect('/'); }
public function compute($id) { $fixture = Fixture::findOrFail($id); $gameweek = $fixture->gameweek; if ($fixture->home_score === null or $fixture->away_score === null) { return redirect('/admin/gameweek/' . $gameweek->id); } $allPredictions = Prediction::where('fixture_id', $fixture->id)->get(); foreach ($allPredictions as $prediction) { $prediction->grade = $prediction->grading($fixture); $boostId = $prediction->user->gameweeks()->where('gameweek_id', $gameweek->id)->first()->pivot->boost_id; if ($boostId == $prediction->fixture->id) { $prediction->grade += 6; } $prediction->save(); } if (!$fixture->isOver()) { $fixture->over = true; $fixture->save(); } return redirect('/admin/gameweek/' . $gameweek->id); }