laravellaravel-excel

laravel import multiple csv to database


I'm using SpartnerNL/Laravel-Excel and I'm trying to import multiple csv files to database.

Controller:

public function importStatement(Request $request) 
    {
        foreach ($request->file('file') as $file) {
        Excel::import(new StatementsImport, $file);
        }
               
        return back();
    }

I tried importing single csv file and it worked using:

public function importStatement(Request $request) 
    {
        Excel::import(new StatementsImport, $file);

        return back();
    }

My problem is when I'm importing multiple csv files.

Blade:

<form action="{{ route('admin.statements.importStatement') }}" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
     <div class="p-6 text-center">
          <input id="file" type="file" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" multiple name="file">
          <button type="submit" >Import</button>
     </div>
</form>

Solution

  • I've solved my problem by following the answer here:

    I edited my blade to:

    <form action="{{ route('admin.statements.importStatement') }}" method="POST" enctype="multipart/form-data">
    {{ csrf_field() }}
         <div class="p-6 text-center">
              <input id="file" type="file" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" multiple name="file[]">
              <button type="submit" >Import</button>
         </div>
    </form>