php - How to properly update multiple values in a table?

I am using Laravel and I have the following problem. When executing a function on my play site, I need to update different values, but in the same table column.
I will give an example: there is a team A and a team B. When a user places a bet on team A, everything is fine, when a user places a bet on team B, everything is also fine, already the third user places a bet on any of the teams, the coefficient of all bets of that team is updated, on which the user made a bet.
For example: On team A the bet is $ 1, the coefficient for the team is recorded as x1, and on the opposite coefficient it becomes x2. Further, if the user places a $ 7.62 bet on team B, the odds are recorded as x1.13. And if the third user places a $ 2 bet on team A, then all odds with a bet on team A change to x3.19, this is how it should be. But the bet on team B in the base is displayed as the old value x1.13, although the odds should change.
That is, how i understand, for any bet on an event wherematch_id
is the same, i need to change the coefficients for all bets after the next bet. How can I do this correctly?
My code, which changes all X bets where the team ID matches:
if ($betsCount > 1) {
$matchUsers = Bets_match::where('team_id', $request->teamID)->get();
foreach ($matchUsers as $matchUser) {
$x = $tournamentMatch->team1_id == $matchUser->team_id ? $tournamentMatch->x1 : $tournamentMatch->x2;
$coef_percent = $x * self::COMMISSION_PERCENT / 100;
$x = $x - $coef_percent;
$matchUser->x = $x;
$matchUser->save();
}
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: integrity constraint violation: 1452 cannot add or update a child row: a foreign key constraint fails
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.