search

Home  >  Q&A  >  body text

How to import multiple Excel files in Laravel using Laravel Excel?

I'm trying to import multiple files in Laravel using Laravel Excel.

I have the following code in my blade file which allows me to select multiple files to upload:

<form action="{{ route('file-import') }}" method="POST" enctype="multipart/form-data">
    @csrf
    <div class="form-group mb-4" style="max-width: 500px; margin: 0 auto;">
        <div class="custom-file text-left">
            <input type="file" name="file" class="custom-file-input" id="customFile" multiple>
            <label class="custom-file-label" for="customFile">Choose file</label>
        </div>
    </div>
    <button class="btn btn-primary">Import data</button>
</form>

In the controller I use the following code:

public function fileImport(Request $request) 
    {   
        
        Excel::import(new LogsImport, $request->file('file')->store('temp'));
        return back();
    }

It works fine, but it only imports the first file I select. I believe I need some kind of foreach statement. I tried the following options:

public function fileImport(Request $request) 
    {   
        foreach($request->file('file') as $f){
            Excel::import(new LogsImport, $f->store('temp'));
        }
        return back();
    }

But using this no files are imported.

I also tried printing $request but I got a huge array and couldn't find anything relevant pointing to the file I uploaded.

Any help would be greatly appreciated. Thanks

P粉536909186P粉536909186319 days ago500

reply all(1)I'll reply

  • P粉696605833

    P粉6966058332023-12-31 07:17:06

    Try using array name="file[]" instead of name="file"

    reply
    0
  • Cancelreply