skip to Main Content

when i want to running my import script, i have a trouble like this. i have replaced excel load() to excel import(), but this soltion doesn’t work. what should i do to slove this trouble?
when i want to running my import script, i have a trouble like this. i have replaced excel load() to excel import(), but this soltion doesn’t work. what should i do to slove this trouble?

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

2

Answers


  1. Chosen as BEST ANSWER

    I've put InterfaceSupplier( as UplSupplierImport ) in appExports. I have another trouble again, the trouble is "Array to string conversion". This is my new InterfaceSupplier( as UplSupplierImport ) that I've put in my app/export. For the new code of the controller was same with you code before:

    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],
            ];
        }
    }
    

    And this is the controller code:

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

  2. Load method is removed from the current version of Laravel Exel. You can consider to do:

    Create a import class:

    namespace AppImports;
    
    use MaatwebsiteExcelConcernsToArray;
    
    class InterfaceSupplier implements ToArray
    {
        public function array(array $row)
        {}
    }
    

    Then:

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

    If you want to use import method instead, reference https://docs.laravel-excel.com/3.1/imports/batch-inserts.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search