简体   繁体   English

如何使用块方法使Laravel Excel上载文件?

[英]How to make File Upload with Laravel excel using chunk method?

public function uploadNotaCorte(Request $request, EstadoRepository $estadoRepository)
{
    $error      = array();
    $path       = $request->file('file')->getRealPath();
    $notasCorte = Excel::load($path, function($reader) {
    })->get();

    $chunk      = $notasCorte->chunk(100);

    foreach ($notasCorte as $key => $notaCorte) {
    //RULES
   }return  $error;
}

** Hi everyone, I'm new to programming and I'm having a hard time implementing the chunk method, so the dodigo above usually works on small files plus larger error files because of the size. **大家好,我是编程的新手,我很难实现块方法,因此,由于大小,上面的dodigo通常适用于小文件和较大的错误文件。 I need to upload a file with 120,000 records and I am trying to use the chunk for this, I do not know what I can do wrong already looked at the documentation more and very simple and I could not solve the problem can anyone help me ??** 我需要上传一个包含120,000条记录的文件,而我正尝试使用该块,我不知道我该怎么做,已经看了更多而且非常简单的文档,我无法解决问题,有人可以帮助我吗? **

Assuming you're using the maatwebsite/excel package, this link should help: http://www.maatwebsite.nl/laravel-excel/docs/import#chunk 假设您使用的是maatwebsite/excel软件包,则此链接应该会有所帮助: http : //www.maatwebsite.nl/laravel-excel/docs/import#chunk

You'll want to change your code to something like this: 您需要将代码更改为以下内容:

public function uploadNotaCorte(Request $request, EstadoRepository $estadoRepository)
{
    $error = array();
    $path = $request->file('file')->getRealPath();

    Excel::filter('chunk')->load($path)->chunk(100, function($results)
    {
        foreach($results as $row)
        {
            // RULES
        }
    });

    return  $error;
}

This isn't tested and I've never used that package (though good to know it exists) so your mileage may vary. 这没有经过测试,而且我从未使用过该软件包(尽管知道它存在),所以您的里程可能会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM