1

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 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 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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.