zaster
5 days ago

Index Page - Number of Queries Increases when Number of Records Increases

Posted 5 days ago by zaster

Number of queries increases when adding more POs Could you suggest a way of avoiding this situation

public function index()
    {                  
        //$pos = PO::all();
        $pos = PO::with(['po_items.vendor_company'])
        ->get();

        
        //Calculate the total of each po
        $po_totals = [];
        $vat_no = ''; //string declaration
        foreach ($pos as $po) 
        { 
            $po_total = 0;
 
            foreach ($po->po_items as $po_item) 
            {
                $item_total = $po_item->qty * $po_item->rate;
                $po_total += $item_total;                                           
            }
            
            if(is_null($po->po_items()->withTrashed()->first()))
            {
                $po_totals[$po->id] = $po_total;
            }       
            elseif($po->po_items()->withTrashed()->first()->vendor_company->vat_no && $po->comp == "PKL")
            {
                $po_totals[$po->id] = ($po_total + $po->vat);
            }                                                                            
            else
            {
                $po_totals[$po->id] = $po_total;
            }
            
        }                        
        return view('employees.pos.po', compact('pos', 'po_totals'));        
    }    

Please sign in or create an account to participate in this conversation.