How to import multiple Excel files in Laravel using Laravel Excel?
P粉536909186
P粉536909186 2023-12-30 09:02:38
0
1
523

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粉536909186
P粉536909186

reply all(1)
P粉696605833

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template