Home > database >  Attempt to read property "bujet" on null
Attempt to read property "bujet" on null

Time:09-26

I'm trying to insert new value and add then update a column value from another table. This code sometimes works but when i refresh the site or logout and login then it show this error "Attempt to read property "bujet" on null" and when i enter again in url it shows another error "The GET method is not supported for this route. Supported methods: POST" How can i solve this issue?

//Controller

    public function reverse(Request $request)
    {
            $biniyojan = new Biniyojan();
            $biniyojan->date =  $request->date;
            $biniyojan->ab =  $request->ab;
            $biniyojan->school =  $request->school;
            $biniyojan->behora =  $request->behora;
            $biniyojan->save();

        if (!empty($request->source)) {
            for ($i = 0; $i < count((array)$request->source); $i  ) {
            $biniyojan_details = new BiniyojanDetails();
             
            $biniyojan_details['biniyojan_id'] = $biniyojan->id;
            $biniyojan_details->school = $request->school;
            $biniyojan_details->source = $request->source[$i];
            $biniyojan_details->kriyakalap = $request->kriyakalap[$i];
            $biniyojan_details->debit_credit = $request->debit_credit[$i];
            $biniyojan_details->debit_credit_type = $request->debit_credit_type[$i];
            $biniyojan_details->cash = $request->cash[$i];
            $biniyojan_details->save();

                //Adding Biniyojan balance in Budgets table
                $getBudgetVal = bajet::where(['kriyakalap' => $biniyojan_details->kriyakalap])->first();
                $newBudget = $getBudgetVal->bujet   $biniyojan_details->cash;
                bajet::where('kriyakalap',$biniyojan_details->kriyakalap)->update(['bujet'=>$newBudget]);  
                //Adding Biniyojan balance in Budgets table
          
            }
        }

     return redirect('biniyojan-reverse')->with('status', 'बिनियोजन उल्टियो !!!');
    }

//route

Route::post('/biniyojan-reverse',[BiniyojanController::class,'reverse']) ->name('reverse')->middleware(['auth','admin']);

CodePudding user response:

My advice would be to check/test each line first to determine the source of the problem.

1.Return the first process result.

public function reverse(Request $request)
{
        $biniyojan = new Biniyojan();
        $biniyojan->date =  $request->date;
        $biniyojan->ab =  $request->ab;
        $biniyojan->school =  $request->school;
        $biniyojan->behora =  $request->behora;
        return $biniyojan->save();              // <----
}

2.Return the first result of the second process.

public function reverse(Request $request)
{
        $biniyojan = new Biniyojan();
        $biniyojan->date =  $request->date;
        $biniyojan->ab =  $request->ab;
        $biniyojan->school =  $request->school;
        $biniyojan->behora =  $request->behora;
        $biniyojan->save();

        if (!empty($request->source)) {
          for ($i = 0; $i < count((array)$request->source); $i  ) {
           $biniyojan_details = new BiniyojanDetails();
         
           $biniyojan_details['biniyojan_id'] = $biniyojan->id;
           $biniyojan_details->school = $request->school;
           $biniyojan_details->source = $request->source[$i];
           $biniyojan_details->kriyakalap = $request->kriyakalap[$i];
           $biniyojan_details->debit_credit = $request->debit_credit[$i];
           $biniyojan_details->debit_credit_type =       $request->debit_credit_type[$i];
           $biniyojan_details->cash = $request->cash[$i];
           return $biniyojan_details->save();  // <--- 
          }
        }
}

3.Fix the WHERE on this part and try to return it to check if it gets any data.

$getBudgetVal = bajet::where(['kriyakalap' => $biniyojan_details->kriyakalap])->first();      //from this


$getBudgetVal = bajet::where('kriyakalap', $biniyojan_details->kriyakalap)->first();      // to this

if ever this can be null you can add Null Coalescing Operator (??) before adding it to the next line of code.

$newBudget = ($getBudgetVal->bujet ?? 0)   $biniyojan_details->cash;

at some case you might be adding a null to any number.

It is possible that it will not solve your problem. However, hopefully it will assist you in determining where the problem may be occurring.

CodePudding user response:

update this line

$getBudgetVal = bajet::where(['kriyakalap' => $biniyojan_details->kriyakalap])->first();

to this

$getBudgetVal = bajet::where('kriyakalap',$biniyojan_details->kriyakalap)->first();

and dump it

dd($getBudgetVal);

and see the result of it

  • Related