Good day to all, I was creating balance system which works like this. When a user creates record having TypeOfAccounting field as Income then the value from SumOfMoney field will be added to balance system(for example if balance was 0 then the value from SumOfMoney field will be added to it). If the user creates record with Expense as a value of TypeOfAccounting field then the value from SumOfMoney field will go in substraction way. I faced the problem with update of the records, that is, the record being Expense and having 10 as a sum spent and thus I have -10 balance but when I update that record and put 5 instead of 10 then balance remains -10. The code:
public function update(Request $request, $id)
{
//Create PersAccounting
$personal=PersonalAccounting::find($id);
$personal->TypeOfAccounting=$request->input('TypeOfAccounting');
$user_id=auth()->user()->id;
$user=User::find($user_id);
$personal->Name=$request->input('Name');
$user->balance-=$personal->SumOfMoney;
$personal->SumOfMoney=$request->input('SumOfMoney');
$personal->Comments=$request->input('Comments');
$user->balance+=$request->input('SumOfMoney');
if($request->input('TypeOfAccounting')=='Expense'){
$user_id=auth()->user()->id;
$user=User::find($user_id);
$personal->Name=$request->input('Name');
$user->balance+=$personal->SumOfMoney;
$personal->SumOfMoney=$request->input('SumOfMoney');
$personal->Comments=$request->input('Comments');
$user->balance-=$request->input('SumOfMoney');
}
$user->save();
$personal->save();
return redirect('/personal')->with('success', "Updated");
}
Please guys if something is not clear to you. let me know