I have written this at the Controller for updating balance
field which exists at the user_wallet
table (which is the pivot table in User
& Wallet
Models - many to many relationship).
$wallet = Wallet::find($wallet_id);
if($value_added_type == '-')
$wallet->users()->updateExistingPivot($user_id,["balance"-=$amount_add_value]);
else
$wallet->users()->updateExistingPivot($user_id,["balance"+=$amount_add_value]);
So I said "balance"-=$amount_add_value
because I needed to calculate balance in this way:
balance = balance - $amount_added_value
And if $value_added_type
is increasing:
balance = balance + $amount_added_value
But now I get this error:
syntax error, unexpected '-=' (T_MINUS_EQUAL), expecting ']'
So how to calculate balance
properly in this case ?
I would really appreciate any idea or suggestion from you guys...
Thanks in advance.
UPDATE #1:
I tried this for updating balance
field:
// Getting balance field in pivot table based on user_id & wallet_id
$bal = Wallet::with("users")->whereHas('users', function ($query) use ($wallet_id,$user_id) {
$query->where('wallet_id',$wallet_id);
$query->where('user_id',$user_id);
});
// updating balance field
$wallet = Wallet::find($wallet_id);
if($value_added_type == '-'){
$wallet->users()->updateExistingPivot($user_id,["balance" => ($bal - $amount_add_value)]);
}else{
$wallet->users()->updateExistingPivot($user_id,["balance" => ($bal + $amount_add_value)]);
}
But now I get this error:
Object of class Illuminate\Database\Eloquent\Builder could not be converted to number