laravel ::在一个表中插入数组的某些值,在另一表中插入其他值

时间:2018-11-12 08:19:42

标签: excel laravel

我正在上传一个excel文件,并且在该excel文件中,标头是两个不同表的字段名称,并且我想将Excel文件中的相应值从同一数组$ 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');
   }


}

2 个答案:

答案 0 :(得分:1)

尝试一下,将数据分成2个数组

    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');
 }


}

答案 1 :(得分:1)

您可以使用Laravel Colletion的onlyexcept方法

$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());