如何将数据从Excel上传和保存到数据库?

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

标签: php mysql excel laravel file-upload

我有一个Excel文件,我想上传数据并将其保存到数据库。如果第一行中的标题是我下面的代码, enter image description here

但是如果标题如下图所示,我的代码是错误的,如果我有下面的表格,如何读取标题

enter image description here

if($request->hasFile('file')){
   $extension = File::extension($request->file->getClientOriginalName());
          
    if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {

        $path = $request->file->getRealPath();
        $data = Excel::load($path, function($reader) {
        })->get();

        if(!empty($data) && $data->count()){

            foreach ($data as $value) {
                $insert['data'] = [
                'no'  => $value->no,
                'name'  => $value->name,
                'ic_no' => $value->ic_no,
                'disburse_date' => $value->disburse_date,
                'product_type_description' => $value->product_type_description,
                'amount_release' => $value->amount_release,
                'amount' => $value->amount,
                'date_disburse' => $value->date_disburse,
                'net_disbursement' => $value->net_disbursement,
                ];
                
                Loandisburse::where('ic', $value['ic_no'])
                   ->where('stage','W11')
                   ->update(array('status_upload'=>'1',
                   'amount_release' => $value['amount_release'],
                   'amount' => $value['amount'],
                   'date_disburse' => $value['date_disburse'], 
                   'net_disbursement' => $value['net_disbursement'],
                   'disburse_date' => $value['disburse_date'],
                   'product_type_description' => $value['product_type_description'],
                   'hash'=> $hash,
                   'month'=> $month,
                   'year'=>$nyear
                  ));
                  }
                }
                 return redirect('/upload/amount');
                }
            }

0 个答案:

没有答案