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

xjreopfe  于 2023-04-13  发布在  其他
关注(0)|答案(2)|浏览(173)

当我想运行我的导入脚本,我有这样的麻烦.我已经取代Excel加载()到Excel导入(),但这个soltion不工作.我应该怎么做来解决这个麻烦?当我想运行我的导入脚本,我有这样的麻烦.我已经取代Excel加载()到Excel导入(),但这个soltion不工作.我应该怎么做来解决这个麻烦?

public function postImport(Request $request)
{
    // $this->validate($request, [
    //     'file'  => 'required|mimes:xls,xlsx'
    //    ]);
  
       $path = $request->file('file')->getRealPath();
  
       $data = Excel::load($path)->get();
    // $data = Excel::toArray([],$path);
       
  
       if($data->count() > 0)
       {
        foreach($data->toArray() as $key => $value)
        {
         foreach($value as $row)
         {
          $insert_data[] = array(
            'nomorUrut' => $row[0],
            'namaSupplier' => $row[1], 
            'singkatanSupplier' => $row[2], 
            'tipeSupplier' => $row[3], 
            'alamatSupplier' => $row[4], 
            'kodePosSupplier' => $row[5], 
            'noTelpSupplier' => $row[6], 
            'noFax' => $row[7],
            'emailSupplier' => $row[8],
            'contakSupplier' => $row[9],
            'hp' => $row[10],
          );
         }
        }
  
        if(!empty($insert_data))
        {
         DB::table('uplSupplier')->insert($insert_data);
        }
       }
       return back()->with('success', 'Excel Data Imported successfully.');
    //   }
nr9pn0ug

nr9pn0ug1#

我已经把接口供应商(作为UplSupplierImport)在app\Exports.我有其他的麻烦了,麻烦是“数组到字符串转换”.这是我的新接口供应商(作为UplSupplierImport),我已经把我的app/export.对于控制器的新代码与你之前的代码相同:

class InterfaceSupplier implements ToArray
{
    public function array(array $row)
    {
        return [
            'nomorUrut' => $row[0],
            'namaSupplier' => $row[1],
            'singkatanSupplier' => $row[2],
            'tipeSupplier' => $row[3],
            'alamatSupplier' => $row[4],
            'kodePosSupplier' => $row[5],
            'noTelpSupplier' => $row[6],
            'noFax' => $row[7],
            'emailSupplier' => $row[8],
            'contakSupplier' => $row[9],
            'hp' => $row[10],
        ];
    }

}

这是控制器代码:

public function postImport(Request $request)
{

    $path = $request->file('file')->getRealPath();
    $insert_data = Excel::toArray(new InterfaceSupplier(), $path);

    if(!empty($insert_data))
    {
        DB::table('uplSupplier')->insert($insert_data);
    }

    return back()->with('success', 'Excel Data Imported successfully.');

}

vxf3dgd4

vxf3dgd42#

Load方法从当前版本的Laravel Exel中删除。您可以考虑这样做:
创建导入类:

namespace App\Imports;

use Maatwebsite\Excel\Concerns\ToArray;

class InterfaceSupplier implements ToArray
{
    public function array(array $row)
    {}
}

然后:

public function postImport(Request $request)
{
    $path = $request->file('file')->getRealPath();
    $data = Excel::toArray(new InterfaceSupplier(), $path);
    
    $data = $data[0];
    $insert_data = [];

    foreach ($data as $row) {
        $insert_data[] = [
            'nomorUrut' => $row[0],
            'namaSupplier' => $row[1],
            'singkatanSupplier' => $row[2],
            'tipeSupplier' => $row[3],
            'alamatSupplier' => $row[4],
            'kodePosSupplier' => $row[5],
            'noTelpSupplier' => $row[6],
            'noFax' => $row[7],
            'emailSupplier' => $row[8],
            'contakSupplier' => $row[9],
            'hp' => $row[10],
        ];
    }

    if (!empty($insert_data)) {
        DB::table('uplSupplier')->insert($insert_data);
    }

    return back()->with('success', 'Excel Data Imported successfully.');
}

如果要使用import方法,请参考https://docs.laravel-excel.com/3.1/imports/batch-inserts.html

相关问题