-4

Good day to all, I faced the error:Use of undefined constant TypeOfAccounting - assumed 'TypeOfAccounting in controller Code in Controller:

 $personal= new PersonalAccounting();
        $personal->TypeOfAccounting=$request->input('TypeOfAccounting');
        $personal->Name=$request->input('Name');
        $personal->SumOfMoney=$request->input('SumOfMoney');
        $personal->user_id= auth()->user()->id;
        $user_id=auth()->user()->id;
        $user=User::find($user_id);
        if($request->input(TypeOfAccounting)=='Income'){
         $request->input('SumOfMoney')+ $user->balance;
        }
        if($request->input(TypeOfAccounting)=='Expense'){
            $request->input('SumOfMoney')-$user->balance;
        }
        $personal->save();
Mirich
  • 333
  • 2
  • 5
  • 19

1 Answers1

1

You are missing quotes for variable name so change this

You are doing mistake in 2 lines

1-if($request->input(TypeOfAccounting)=='Income')
2-if($request->input(TypeOfAccounting)=='Expense')

variables should be in quotation marks like shown below

1-if($request->input('TypeOfAccounting')=='Income')
2-if($request->input('TypeOfAccounting')=='Expense')

To sum and save in database do this:

    if($request->input(TypeOfAccounting)=='Income'){
     $user->balance = $request->input('SumOfMoney')+ $user->balance;
     //sum user input and old balance and save into balance
    }
    if($request->input(TypeOfAccounting)=='Expense'){
        $user->balance = $request->input('SumOfMoney')-$user->balance;
        //difference user input and old balance and save into balance
    }
    $user->save();
   //now save $user object to save changes in database.
Afraz Ahmad
  • 5,193
  • 28
  • 38
  • Again me Afraz, first of all thank you for your solution, look have a question to you. When I insert new record into database I want number that is inside $request->input(TypeOfAccounting) to User's column called balance by using $request->input('SumOfMoney')+ $user->balance; Is this correct. I really need your help – Mirich Sep 09 '18 at 17:21
  • Most welcome bro. $request->input('SumOfMoney')+ $user->balance; do you want to sum these values and save in column in database ? – Afraz Ahmad Sep 09 '18 at 17:28
  • Yes, So, if field by the name TypeOfAccounting has value Income I want to take value of SumOfMoney field and add it to $user->balance which remains 0 even if I add new record with Income(TypeOfAccounting) – Mirich Sep 09 '18 at 17:31
  • I have updated my answer see it – Afraz Ahmad Sep 09 '18 at 17:34
  • 1
    Wow, Thank you dude may Allah bless you :) – Mirich Sep 09 '18 at 17:35
  • Most welcome bro and stay blessed – Afraz Ahmad Sep 09 '18 at 17:36