Home » excel » excel – laravel:: inserting some value of an array in one table and other values in the other table

excel – laravel:: inserting some value of an array in one table and other values in the other table

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am uploading an excel file and in that excel file the headers are the field names of two different table and I want to insert the corresponding values from that Excel files to those two different table from a same array $dataArray:

    public function sapinvoiceuploads(Request $request) {
    if($request->hasFile('target_file')){ 
       $originalfile=$request->file('target_file')->getRealPath();
       $dataall = Excel::load($originalfile, function ($reader) {
            })->get();
           // echo '<pre>';
           foreach ($dataall->toArray() as $key => $row) {
              // print_r($row);
               if(!empty($row['party_id'])) {

                        $dataArray[] =
                                    [
                                    'sapinvno' => $row['sapinvno'], //Invoice No.
                                    'Invoice Date' => $row['Invoice Date'],
                                    //'Customer Po No.' => $row['Customer Po No.'],
                                    //'Custome Po Date' => $row['Custome Po Date'],
                                    //'Payment Due Date' => $row['Payment Due Date'],
                                    //'Payment Terms' => $row['Payment Terms'],
                                    'productname' => $row['productname'],// Description of Goods
                                    'shippingcharge' => $row['shippingcharge'],//Handling Charges
                                    'productquantity' => $row['productquantity'],//Quantity
                                    'bundle' => $row['bundle'],
                                    'hsn' => $row['hsn'],
                                    'productprice' => $row['productprice'],
                                    'ordersubtotal' => $row['ordersubtotal'],
                                    'gst' => $row['gst'],//IGST Amount
                                    'ordertotal' => $row['ordertotal'],

                                ];       
                    }
                }
           if (!empty($dataArray) && count($dataArray)>0) {
            invoice::insert($dataArray);
            invoiceproducts::insert($dataArray);
           }
           return Redirect::back()->with('success', 'File Uploaded Successfully');
       } 
       else{
        return Redirect::back()->with('error', 'Please Choose A File to Upload');
   }


}
How to&Answers:

Try this, you split your data into 2 arrays

    public function sapinvoiceuploads(Request $request) {
if($request->hasFile('target_file')){ 
   $originalfile=$request->file('target_file')->getRealPath();
   $dataall = Excel::load($originalfile, function ($reader) {
        })->get();
       // echo '<pre>';
       foreach ($dataall->toArray() as $key => $row) {
          // print_r($row);
           if(!empty($row['party_id'])) {

                    $dataArray[] =
                                [
                                'sapinvno' => $row['sapinvno'], //Invoice No.
                                'Invoice Date' => $row['Invoice Date'],
                                'productname' => $row['productname'],// Description of Goods
                                'shippingcharge' => $row['shippingcharge'],//Handling Charges
                                'productquantity' => $row['productquantity'],//Quantity
                                'bundle' => $row['bundle'],
                                'hsn' => $row['hsn'],
                                'productprice' => $row['productprice'],
                                'ordersubtotal' => $row['ordersubtotal'],
                                'gst' => $row['gst'],//IGST Amount
                                'ordertotal' => $row['ordertotal'],

                            ];   
                    $dateArray2[] = 
                            [
                                'Customer Po No.' => $row['Customer Po No.'],
                                'Custome Po Date' => $row['Custome Po Date'],
                                'Payment Due Date' => $row['Payment Due Date'],
                                'Payment Terms' => $row['Payment Terms'],
                            ]    
                }
            }
       if (!empty($dataArray) && count($dataArray)>0 && !empty($dataArray2) && count($dataArray2)>0) {
        invoice::insert($dataArray);
        invoiceproducts::insert($dataArray2);
       }
       return Redirect::back()->with('success', 'File Uploaded Successfully');
   } 
   else{
    return Redirect::back()->with('error', 'Please Choose A File to Upload');
 }


}

Answer:

you can use only and except methods of the Laravel Colletion

$excluded = ['Customer Po No.', 'Custome Po Date', 'Payment Due Date', 'Payment Terms'];
invoice::insert(collect($dataArray)->except($excluded)->toArray());
invoiceproducts::insert(collect($dataArray)->only($excluded)->toArray());