SimonAngatia
1 month ago
776
45
Laravel

Model::decrement() and Model::increment() not working efficiently

Posted 1 month ago by SimonAngatia

Hey guys, I am getting data from a request and I have created a model observer to do some computations on the data after it has been saved. The computations are like incrementing the user balance based on the amount that came in the request. The shocking thing however is that this is working for some instances and not others, even though the amount is received and saved in the database. What could be happening?

public function created(XpgTransaction $xpgTransaction)
    {   $userId = $xpgTransaction->user_id;
        $user = User::where('id', $userId)->first();
        $userBalance = $user->casino_bakiye;
        if($xpgTransaction->type =="debit"){
            $debitBalance = $xpgTransaction->amount;
            $user->decrement('casino_bakiye', $debitBalance);
        }
        if($xpgTransaction->type =="credit"){
            $creditBalance = $xpgTransaction->amount;
            $user->increment('casino_bakiye', $creditBalance);
        }

    }

Please sign in or create an account to participate in this conversation.