-1

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

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
  • Follow this instruction https://stackoverflow.com/questions/66811609/cant-insert-into-pivot-table/66812016#66812016 – A.A Noman Jul 18 '21 at 04:20

1 Answers1

0

["xxx" -= 123] or ["xxx" += 123] is not a valid PHP syntax. It must be ["xxx" => ($balance - 123)] or ["xxx" => ($balance + 123)].

You are confusing with a variable:

$variable -= 123;
$variable += 123;

The use of [ ] is for arrays, is same as array():

$array1 = [
    "index1" => 123
];

echo $array1["index1"];

That would print 123...


Edit:

To your new update, you are trying to use $bal as a number value, but that is a Builder as the error states, you must do ->first() so you get the Model and then you can do $bal->balance.

So, do this:

// 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);
})->first();

// updating balance field    
$wallet = Wallet::find($wallet_id);
if($value_added_type == '-'){
    $wallet->users()->updateExistingPivot($user_id, ["balance" => ($bal->balance - $amount_add_value)]);
}else{
    $wallet->users()->updateExistingPivot($user_id, ["balance" => ($bal->balance + $amount_add_value)]);
}

Or use the corresponding field related to balance. I used ->balance but I have no idea what your Model/Database is.

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
  • I tried this and added an **UPDATE #1** to the question. I get `Object of class Illuminate\Database\Eloquent\Builder could not be converted to number` error! –  Jul 18 '21 at 03:49
  • @cerealikaeme see the update I have made in my answer – matiaslauriti Jul 18 '21 at 04:30