如何使用Laravel Excel在Laravel中导入多个Excel文件?

发布于 2025-02-08 06:46:50 字数 1256 浏览 2 评论 0原文

我正在尝试使用Laravel Excel在Laravel中导入多个文件。

我在刀片文件中有以下代码,该代码允许我选择多个文件:

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

在控制器中,我使用以下代码:

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

它可以正常工作,但它仅导入我选择的第一个文件。 我相信我需要某种言论。我尝试了以下选项:

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

但是使用此文件没有导入。

我还尝试打印$请求,但我得到了一个巨大的数组,但找不到任何相关的指向我上传的文件。

任何帮助将不胜感激。谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天荒地未老 2025-02-15 06:46:50

尝试使用数组名称=“ file []”而不是名称=“文件”

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文