Home > Software design >  How to import multiple excel files in Laravel using Laravel Excel?
How to import multiple excel files in Laravel using Laravel Excel?

Time:06-17

I am 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 be uploaded:

<form action="{{ route('file-import') }}" method="POST" enctype="multipart/form-data">
    @csrf
    <div  style="max-width: 500px; margin: 0 auto;">
        <div >
            <input type="file" name="file"  id="customFile" multiple>
            <label  for="customFile">Choose file</label>
        </div>
    </div>
    <button >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 option:

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

But using this no file is getting imported.

I also tried printing $request but I get a huge array and I can't find anything relevant that points to the files I uploaded.

Any help would be appreciated. Thanks

CodePudding user response:

try to use array name="file[]" instead of name="file"

  • Related