【问题标题】:Laravel5.3 maatwebsite/excel Read value from excel & insert into databaseLaravel 5.3 maatwebsite/excel 从 excel 读取值并插入数据库
【发布时间】:2016-11-13 10:28:12
【问题描述】:

这是我的 Excel 数据表类型:

这是我的表架构:

这是我导入数据的方法:

public function saveActivationInfoExcel(Request $request)
    {

        if($request->hasFile('import_file')){
            $path = $request->file('import_file')->getRealPath();

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

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

                foreach ($data->toArray() as $key => $value) {
                    if(!empty($value)){
                        foreach ($value as $v) {        
                            $insert[] = [
                                        'mobile_no' =>$v['mobile_no'], 
                                        'model' => $v['model'],
                                        'serial_1' => $v['serial_1'],
                                        'serial_2' => $v['serial_2'],
                                        'activation_date' => $v['activation_date'],
                                        ];
                        }
                    }
                }

                dd($insert);
                if(!empty($insert)){
                    //ProductActivationInfo::insert($insert);
                    return back()->with('success','Insert Record successfully.');
                }

            }

        }

        return back()->with('error','Please Check your file, Something is wrong there.');
    }

现在我的问题是,当我尝试导入数据时,我得到了这个 dd($insert) 错误

Illegal string offset 'mobile_no'

'serial_1' => '3.5194508009307E+14', 
'serial_2' => '3.5194508009307E+14', 
'activation_date' => object(Carbon), null)

看看 serial_1 和 serial_2 显示不同的值而不是实际的 excel 值。 activation_date 值也为空。

非常感谢您提出宝贵的建议,以解决有关使用“maatwebsite/excel”laravel 库读取数据的问题。

【问题讨论】:

  • 您使用的是 .xls 还是 .csv?我相信使用 .csv 会解决问题... :)
  • 检查您插入的日期格式应该是 mysql :yyyy/m/d 我认为不确定

标签: laravel laravel-5.3 maatwebsite-excel


【解决方案1】:

使用 CSV 文件导入数据,并将 serial_1 和 serial_2 列格式更改为 分数。并更改activation_date列的格式。

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 2020-09-25
    • 1970-01-01
    • 2017-11-12
    • 2021-10-08
    • 2021-09-17
    相关资源
    最近更新 更多