如何在Laravel中导入CSV文件

时间:2019-07-16 06:12:29

标签: laravel-5

我正在尝试在项目中导入CSV文件,但出现此错误:

  

调用未定义的方法Maatwebsite \ Excel \ Excel :: load()

我正在使用Excel版本3.0并尝试安装2.0.1,但无法正常工作。

这是我的代码//控制器

public function import(Request $request)
    {

        if ($request->hasFile('imported-file')) {
            $path = $request->file('imported-file')->getRealPath();
            $data = Excel::load($path, function ($reader) {})->get();
            if (!empty($data) && $data->count()) {
                foreach ($data->toArray() as $key => $value) {
                    if (!empty($value)) {
                        Item::insert($value);
                        return back();
                    }
                }
            }
        }
    }

0 个答案:

没有答案