如何在Laravel中获取特定的列[Maatwebsite / Laravel-Excel]

问题描述 投票:0回答:1

我正在导入的文件有数千条记录,这导致我的网络变慢。在将其插入数据库之前,我只想阅读我需要的那些列。处理文件时,应首先搜索那些列而不是整个文件,然后获取这些列的行

Excel::load($path, function($reader) {

//Getting headers using this


       $headers = $reader->first()->keys()->toArray();

//This is the array of required columns

       $headings = array('registrant_name','registrant_address','registrant_phone','registrant_zip','registrant_email','registrant_country','registrant_state','registrant_city');


});

读取文件后的数据插入。

 if($data->count() > 0)
             {
              foreach($data->toArray() as $value)
              {
                $insert[] = array(
                 'registrant_name'  => $value['registrant_name'],
                 'registrant_address'   => $value['registrant_address'],
                 'registrant_phone'   => $value['registrant_phone'],
                 'registrant_zip'   => $value['registrant_zip'],
                 'registrant_email'   => $value['registrant_email'],
                 'registrant_country'   => $value['registrant_country'],
                 'registrant_state'   => $value['registrant_state'],
                 'registrant_city'   => $value['registrant_city']

                );                
               }
              }


      if(!empty($insert))
      {
       DB::table('customers')->insert($insert);
      }    
php excel laravel phpexcel phpoffice
1个回答
0
投票

在集合中,您可以检查方法 documentation

$headers = $reader->first()->only(['registrant_name','registrant_address','registrant_phone','registrant_zip','registrant_email','registrant_country','registrant_state','registrant_city']);

您也可以使用chunk,将数据插入到比大集合小的集合中。

您可以使用App \ Import类来更新帖子,以便为您提供更多帮助。

© www.soinside.com 2019 - 2024. All rights reserved.