Home > Enterprise >  I want to access id of MySql invoice table into another MySql invoice_ products table. I am able to
I want to access id of MySql invoice table into another MySql invoice_ products table. I am able to

Time:08-28

Controller:

           if (InvoiceProduct::where('product_name', '=', $file['products'])->exists()) {   
        }else {
            for($x = 0; $x < count($file['products']); $x  )
            { 
                $pstore = new InvoiceProduct;
                $pstore->product_name = $file['products'][$x];
                // $pstore->invoice_id = $[$x]; (i need to insert this ID from Invoice table)
                $pstore->quantity =$file['count'][$x];
                $pstore->barcode=$file['scan'][$x];
                $pstore->vendor_code =$file['vn'][$x];
                $pstore->save(); 
             }
            }

I want this row to display in another table

i want this row to display in another table

I want to store that id in invoice_id

here I want to that id in this row

but the thing is I have multiple files in the Invoice table and in each file I have multiple products. I want to assign the id from the invoice table to its respective product. One id can have multiple products. Thanks for your time!

Complete function:

    public function index(){
    $path = public_path('files\pending');
    $allfiles = scandir($path);
    $allfiles = array_diff(scandir($path), array('.', '..'));
    $files = array();
    foreach ($allfiles as $filename) {
        $file= File::get(public_path('files/pending/'.$filename));
        $data = explode('~', $file);
        $CustomerName = "";
        $PO = "";
        $address ="";
        $city ="";
        $products= array();
        $id= array();
        //
        $count= array();
        $scan= array();
        $ca= array();
        $vn= array();
        $unit= array();
        $sctype= array();
        //
        foreach($data as $d){
            $row = explode("*",$d);
            $row = str_replace("\r\n","",str_replace("\r","",str_replace("\n","",$row)));

            if($row[0] == "N1") {
                $name= $row[2];
                $id =  $row[4]; 


            }elseif($row[0] == "N9") {
                $PO= $row[2];
            }
            elseif($row[0] == "N3") {
                $address= $row[1];
               
            }elseif($row[0] == "N4") {
                $city= $row[1];
           
            }elseif($row[0] == "PID") {
                $products[] = $row[5];
           
            }elseif($row[0] == "PO1") {
                $count[] = $row[2];
                $scan[] = $row[7];
                $ca[] = $row[4];
                $vn[] = $row[9];
                $unit[] = $row[3];
                $sctype[] = $row[6];
             
           
            }
        }
        $files[] = [
            "file_name"=>$filename,
            "customer_name"=>$name,
            "po"=>$PO,
            "loc"=>$address,
            "city"=>$city,
            "products"=> $products,
            "id"=>$id,
            "count"=> $count,
            "scan"=> $scan,
            "ca"=> $ca,
            "vn"=> $vn,
            "unit"=> $unit,
            "sctype"=> $sctype,
      

        ];
        $merge = array_merge_recursive(
            array_combine ($products , $count),
            array_combine($products , $scan),
            array_combine($products , $ca),
            array_combine($products , $vn),
            array_combine($products , $unit),
            array_combine($products , $sctype),
          

        );


    }
      foreach($files as $file){
            
      if (Invoice::where('file_name', '=', $file['file_name'])->exists()) {
                
            }else {
                
            $store = new Invoice;
            $store->file_name = $file['file_name'];
            $store->name = $file['customer_name'];
            $store->po_no = $file['po'];
            $store->address = $file['city'];
            $store->total_items = count($file['products']);
            $store->save();
           
        }

        if (InvoiceProduct::where('product_name', '=', $file['products'])->exists()) {
                
        }else {
            
            for($x = 0; $x < count($file['products']); $x  )
            { 

                $pstore = new InvoiceProduct;
                $pstore->product_name = $file['products'][$x];
                $pstore->invoice_id = $store->id [$x];
                $pstore->quantity =$file['count'][$x];
                $pstore->barcode=$file['scan'][$x];
                $pstore->vendor_code =$file['vn'][$x];
                $pstore->save();
              
             }
            }

    }



    return view('welcome' , compact('files'));
}

CodePudding user response:

I don't know exactly $file['products'] what is, so you can try this:

foreach($files as $file){
    //return invoice or new one
    $invoice = Invoice::firstOrNew([
        'file_name' =>  $file['file_name']
    ]);
    $invoice->file_name = $file['file_name'];
    $invoice->name = $file['customer_name'];
    $invoice->po_no = $file['po'];
    $invoice->address = $file['city'];
    $invoice->total_items = count($file['products']);
    $invoice->save();

    foreach (count($file['products']) as $key => $product) {
        $invoiceProduct = new InvoiceProduct;

        //if $product is object
        $invoiceProduct->product_name = $product->name;
        //if $product is array
        $invoiceProduct->product_name = $product;

        $invoiceProduct->invoice_id = $invoice->id;
        $invoiceProduct->quantity = $file['count'][$key];
        $invoiceProduct->barcode = $file['scan'][$key];
        $invoiceProduct->vendor_code = $file['vn'][$key];
        $invoiceProduct->save();
    }
}

CodePudding user response:

I was doing a little mistake that took me 5 hours to realize

public function index(){
    $path = public_path('files\pending');
    $allfiles = scandir($path);
    $allfiles = array_diff(scandir($path), array('.', '..'));
    $files = array();
    // $invid = Invoice::get('id');
    foreach ($allfiles as $filename) {
        $file= File::get(public_path('files/pending/'.$filename));
        $data = explode('~', $file);
        $CustomerName = "";
        $PO = "";
        $address ="";
        $city ="";
        $products= array();
        $id= array();
        //
        $count= array();
        $scan= array();
        $ca= array();
        $vn= array();
        $unit= array();
        $sctype= array();
        $invid=  1; (I declared variable over here)
        //
        foreach($data as $d){
            $row = explode("*",$d);
   $row = str_replace("\r\n","",str_replace("\r","",str_replace("\n","",$row)));

            if($row[0] == "N1") {
                $name= $row[2];
                $id =  $row[4]; 

            }elseif($row[0] == "N9") {
                $PO= $row[2];
            }
            elseif($row[0] == "N3") {
                $address= $row[1];
               
            }elseif($row[0] == "N4") {
                $city= $row[1];
           
            }elseif($row[0] == "PID") {
                $products[] = $row[5];
           
            }elseif($row[0] == "PO1") {
                $count[] = $row[2];
                $scan[] = $row[7];
                $ca[] = $row[4];
                $vn[] = $row[9];
                $unit[] = $row[3];
                $sctype[] = $row[6];
               
            }
        }
        $files[] = [
            "file_name"=>$filename,
            "customer_name"=>$name,
            "po"=>$PO,
            "loc"=>$address,
            "city"=>$city,
            "products"=> $products,
            "id"=>$id,
            "count"=> $count,
            "scan"=> $scan,
            "ca"=> $ca,
            "vn"=> $vn,
            "unit"=> $unit,
            "sctype"=> $sctype,
        ];
        $merge = array_merge_recursive(
            array_combine ($products , $count),
            array_combine($products , $scan),
            array_combine($products , $ca),
            array_combine($products , $vn),
            array_combine($products , $unit),
            array_combine($products , $sctype),
    
        );
    }
    foreach($files as $file){
            if (Invoice::where('file_name', '=', $file['file_name'])->exists()) {
                
            }else {                    
            $store = new Invoice;
            $store->file_name = $file['file_name'];
            $store->name = $file['customer_name'];
            $store->po_no = $file['po'];
            $store->address = $file['city'];
            $store->total_items = count($file['products']);
            $store->save();
            $invid = $store->id; (get the id of Invoce like this)            
        }

        if (InvoiceProduct::where('product_name', '=', $file['products'])->exists()) {
                
        }else {
            
            for($x = 0; $x < count($file['products']); $x  )
            { 

                $pstore = new InvoiceProduct;
                $pstore->product_name = $file['products'][$x];
         $pstore->invoice_id = $invid;(and insert that invoice id like this)
                $pstore->quantity =$file['count'][$x];
                $pstore->barcode=$file['scan'][$x];
                $pstore->vendor_code =$file['vn'][$x];
                $pstore->save();
             }
            }
    }
    return view('welcome' , compact('files'));
}
  • Related