Need Logical Help to Store the New Balance from Debit or Credit

I need logical help to make this "New Balance" on Debit-Credit Transection Type. How do I code from TextInput to store the old+new amount as new balance? Also this field could be hidden in form.
6 Replies
JibayMcs
JibayMcs12mo ago
- Make your "Amount" and "New Balance" fields reactive - Add afterStateUpdate() method to your "New balance" field (don't forget Get $get, Set $set to retreive the "Amount" field value) Make your maths here to set your new balance like $set('new_balance' $newBalanceResult) And voilà !
Abdur Razzaque
Abdur Razzaque12mo ago
is this the right way am I going please?
TextInput::make('balance')
->label('New Balance')
// ->disabled()
->placeholder('New amount+old balance')
->afterStateUpdated(function (Get $get, Set $set, ?bool $old, ?bool $state) {
// $set(50,);
return $old;
}),
TextInput::make('balance')
->label('New Balance')
// ->disabled()
->placeholder('New amount+old balance')
->afterStateUpdated(function (Get $get, Set $set, ?bool $old, ?bool $state) {
// $set(50,);
return $old;
}),
JibayMcs
JibayMcs12mo ago
Not totally: Something like this for your "Amount" field
->afterStateUpdated(function(Get $get, Set $set, $state) {
$oldBalance = // get your old balance here, but don't know how you store it
$set('new_balance', $oldBalance - $get('amount'));
})
->reactive() // Don't forget reactivity or your "Amount" and "New balance" fields
->afterStateUpdated(function(Get $get, Set $set, $state) {
$oldBalance = // get your old balance here, but don't know how you store it
$set('new_balance', $oldBalance - $get('amount'));
})
->reactive() // Don't forget reactivity or your "Amount" and "New balance" fields
Abdur Razzaque
Abdur Razzaque12mo ago
Should I add ->reactive() in both 'amount' and 'new balance' field?
JibayMcs
JibayMcs12mo ago
Yep
Abdur Razzaque
Abdur Razzaque12mo ago
I tried this code:
TextInput::make('amount')
->numeric()->required()
->afterStateUpdated(function (Get $get, Set $set, $state) {
$oldBalance = Balance::latest('id')->first();
$set('balance', $oldBalance + $get('amount'));
})
->reactive(),
TextInput::make('balance')
->label('New Balance')
->disabled()
->reactive(),
TextInput::make('amount')
->numeric()->required()
->afterStateUpdated(function (Get $get, Set $set, $state) {
$oldBalance = Balance::latest('id')->first();
$set('balance', $oldBalance + $get('amount'));
})
->reactive(),
TextInput::make('balance')
->label('New Balance')
->disabled()
->reactive(),
But it's 1. not giving me the last inserted value on balance filed 2. not inserting the value that I pass manually to the New balance field.