mycloudcms
9 months ago

Update one to many

Posted 9 months ago by mycloudcms

Hi, I need assistance how to update one to many relationship, my save works fine and my update code is below:

    public function update(Request $request, $id)
    {
        $invoice = Invoice::find($id);
        $invoice->invoicedate= $request->get('txtInvoiceCreationDate');
        $invoice->invoicetypeid= 5; 
        $invoice->save();

        $report = $request->input('txtInvoiceProduct');
        $items= isset($report['Id'])?$report['Id']:[];

         


        foreach($items as $key=> $item){

            $invoice->invoiceitem()->update([

                    'invoiceitemproductid'=>$item,                   
                    'invoiceitempriceperunit' => isset($report ['UnitPrice'][$key])?$report['UnitPrice'][$key]:0

            ]);

        }

        return redirect('/invoicetest')->with('success','invoice updated');
    }

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