/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($id, Requests\SessionSetRequest $request) { $sessionSet = SessionSet::with('session.exercise')->findOrFail($id); $sessionSet->one_rep_max = $this->calculateOneRepMax($request->weight_lifted, $request->number_of_reps); // If the user is updating the weight and reps with lower values, // then they likely failed the set (especially if the set was // generated from a plan). However, if it was not generated from a plan // this could be an incorrect assumption... if ($request->weight_lifted < $sessionSet->weight_lifted or $request->number_of_reps < $sessionSet->number_of_reps) { $sessionSet->update(['failed' => 1]); } $sessionSet->update($request->all()); // refactor this code; create function in ExerciseController to update exercise best_one_rep_max $exercise = Auth::user()->exercises()->findOrFail($sessionSet->session->exercise_id); $bestOneRepMax = SessionSet::join('sessions', 'sessions.id', '=', 'session_sets.session_id')->where('sessions.exercise_id', '=', $exercise->id)->max('one_rep_max'); $exercise->update(['best_one_rep_max' => $bestOneRepMax]); return back()->with('status', 'Successful Update!'); }