2016-08-31 7 views
0

Laravel Excelリポジトリを使用して、LaravelのExcelファイル(.csv)のデータを使用してSQLデータベーステーブルを更新しようとしています。

My Controller関数は、Excelファイルの内容を返しますが、配列は2(604)になります。

したがって、私は行末\ tを私の関数に追加しなければならないと思います。
しかし、私はどのように知りません。ここで

Laravel Excelのインポート.csv行の終了が認識されない



は、私がこれまで知っているものです:

コントローラ

public function uploadExcel() 
{ 
     Excel::load(Input::file('import_file'), function ($reader) { 

     foreach ($reader->toArray() as $value) { 
      $insert[] = [ 
      'member_title' => $value->member_title, 
      'member_first_name' => $value->member_first_name, 
      'member_name_affix' => $value->member_name_affix, 
      'member_last_name' => $value->member_last_name, 
      'member_private_address' => $value->member_private_address, 
      'member_private_zip_code' => $value->member_private_zip_code, 
      'member_private_location' => $value->member_private_location, 
      'member_private_phone' => $value->member_private_phone, 
      'member_private_mobile' => $value->member_private_mobile, 
      'member_private_fax' => $value->member_private_fax, 
      'member_private_mail' => $value->member_private_mail, 
      'member_business_position' => $value->member_business_position, 
      'member_business_name' => $value->member_business_name, 
      'member_business_address' => $value->member_business_address, 
      'member_business_zip_code' => $value->member_business_zip_code, 
      'member_business_location' => $value->member_business_location, 
      'member_business_area_code' => $value->member_business_area_code, 
      'member_business_phone' => $value->member_business_phone, 
      'member_business_fax' => $value->member_business_fax, 
      'member_business_mobile' => $value->member_business_mobile, 
      'member_business_mail' => $value->member_business_mail, 
      'member_join_date' => $value->member_join_date, 
      'extra' => $value->extra 
      ]; 
     } 
    }); 
    if(!empty($insert)) { 
     die(var_dump($insert)); <-- puts out the array for testing 
     DB::table('members')->insert($insert); 
    } 
    return redirect('index.index'); 
} 


私は正しい行末を認識するために私のプロジェクトにこれを追加する必要がありますofficial documentationによると、 :

class UserListImport extends \Maatwebsite\Excel\Files\ExcelFile { 
    protected $lineEnding = '\t'; 
} 



私の勘が正しいとドキュメントから、このスニペットは私の問題を解決したい場合:私は、ドキュメントからのコードが含まれているこのファイルを、作成しなければならないの



ファイルを有効にするために何かを変更する必要がありますか?


私はララベルにとって本当に新しく、どんな助けでも非常に感謝しています!



UPDATE


エラーメッセージ

Class App\UserListImport contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Maatwebsite\Excel\Files\ExcelFile::getFile)

APP/UserListImport.php

namespace App; 

    class UserListImport extends \Maatwebsite\Excel\Files\ExcelFile { 
    protected $lineEnding = '\t'; 

    public function loadExcel() { 
     Excel::load(Input::file('import_file'), function ($reader) { 

      foreach ($reader->toArray() as $value) { 
       $insert[] = [ 
       'member_title' => $value->member_title, 
       'member_first_name' => $value->member_first_name, 
       'member_name_affix' => $value->member_name_affix, 
       'member_last_name' => $value->member_last_name, 
       'member_private_address' => $value->member_private_address, 
       'member_private_zip_code' => $value->member_private_zip_code, 
       'member_private_location' => $value->member_private_location, 
       'member_private_phone' => $value->member_private_phone, 
       'member_private_mobile' => $value->member_private_mobile, 
       'member_private_fax' => $value->member_private_fax, 
       'member_private_mail' => $value->member_private_mail, 
       'member_business_position' => $value->member_business_position, 
       'member_business_name' => $value->member_business_name, 
       'member_business_address' => $value->member_business_address, 
       'member_business_zip_code' => $value->member_business_zip_code, 
       'member_business_location' => $value->member_business_location, 
       'member_business_area_code' => $value->member_business_area_code, 
       'member_business_phone' => $value->member_business_phone, 
       'member_business_fax' => $value->member_business_fax, 
       'member_business_mobile' => $value->member_business_mobile, 
       'member_business_mail' => $value->member_business_mail, 
       'member_join_date' => $value->member_join_date, 
       'extra' => $value->extra 
       ]; 
      } 
     }); 
     if(!empty($insert)) { 
      die(var_dump($insert)); 
      DB::table('members')->insert($insert); 
     } 
     return redirect('index.index'); 
    } 
} 


コントローラ

use Maatwebsite\Excel\Facades\Excel; 
use App\UserListImport; 

public function uploadExcel() 
{ 
    UserListImport::loadExcel(); 
} 

答えて

0

この\t手段集計(単語間のギャップ)。行末の場合、次の文字の組み合わせが使用されます'\n', '\r\n'。オペレーティングシステムによって異なります。 '\n'はUnix/Linux/Mac OS用、'\r\n'はWindowsファミリ用です。

+0

ありがとうございました!しかし、どのように '\ t'を行末として使用するかをラーベール関数に指示するのですか? – Schwesi

+0

なぜあなたはそれを行う必要がありますか? @kringeltorte –

+0

私は、関数に '\ n'が行末でなければならないと伝えたいとします。どうすればいい? – Schwesi

1

ドキュメントでは、Excelfileに拡張された新しいカスタムクラスを作成したと想定していました。

class UserListImport extends \Maatwebsite\Excel\Files\ExcelFile 

はこれを意味します。

通常、私のアプリケーションに必要なものには、自分のアプリケーションの名前空間フォルダを追加することをお勧めします。

この場合、私はコントローラーから何かを取り出し、自分のクラスを呼び出してInput::file('import_file')に処理します。そのクラスは、ライブラリがファイルとやりとりする方法を上書きするためにその属性を置く場所です。

+0

ありがとうございました!!! app/UserListImport.phpの下にUserListImportクラスを作成したとしましょう。そして、コントローラから何かを取り出して、どのように正確にそのクラスを呼び出すのですか? – Schwesi

+1

別のクラスを拡張すると、クラスはそのメソッドを継承します。カスタムメソッド 'nameItWhateverYouWant'を追加して' $ this-> load() 'メソッドを呼び出す必要があります。それ以外の場合は' parent :: load() 'を呼び出してください。その 'load()'メソッドはあなたのコントローラで呼び出された 'Excel :: load()'のように動作します。私の鼻の下にコードを持っていない私は確信が持てません、私たちにフィードバックを与えてください。 – phaberest

+1

コントローラの一番上にある 'Use App \ UserListImport;'を覚えておいて、 'UserListImport :: theMethodNameYouChose()'のように呼び出してください。 'UserListImport'では' namespace App;を使用して先頭に名前空間を付けなければなりません – phaberest

関連する問題