0

我在实现队列(作业)以从 excel 表(cyber-duck/laravel-excel 包)读取数据并存储在数据库中时遇到异常,该文件已存储在我的计算机中。

一个例外如下:

Box\Spout\Common\Exception\IOException: Could not open C:\xampp\htdocs\eluminalearningtest\public\uploads/ for reading! (Could not open C:\xampp\htdocs\eluminalearningtest\public\uploads/ for reading.) in C:\xampp\htdocs\eluminalearningtest\vendor\box\spout\src\Spout\Reader\ReaderAbstract.php:130
Stack trace:
#0 C:\xampp\htdocs\eluminalearningtest\vendor\cyber-duck\laravel-excel\src\Importer\AbstractSpreadsheet.php(165): Box\Spout\Reader\ReaderAbstract->open('C:\\xampp\\htdocs...')
#1 C:\xampp\htdocs\eluminalearningtest\vendor\cyber-duck\laravel-excel\src\Importer\AbstractSpreadsheet.php(73): Cyberduck\LaravelExcel\Importer\AbstractSpreadsheet->open()
#2 C:\xampp\htdocs\eluminalearningtest\app\Jobs\BulkUploadJob.php(38): Cyberduck\LaravelExcel\Importer\AbstractSpreadsheet->getCollection()
#3 [internal function]: App\Jobs\BulkUploadJob->handle()
.
.
.

#38 C:\xampp\htdocs\eluminalearningtest\artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#39 {main}

控制器(BulkUploadController),它用于将文件上传到我们可以从该文件中获取数据集合并发送用于调度作业的地方

public function store(Request $request)
    {
        $fileName = time() . '.' . $request->uplodedfile->extension();
        $upload_status = $request->uplodedfile->move(public_path('uploads'), $fileName);

        $excel = LaravelExcelImporterFacade::make('Excel');

        $file_path = public_path('uploads' . DIRECTORY_SEPARATOR . $fileName);

        BulkUploadJob::dispatch($file_path)->delay(now()->addSeconds(10));

        return back()->with('success', 'You have successfully uploaded file.')
            ->with('file', $fileName);
    }

Created Job (BulkUploadJob),它用于将excel文件数据处理到我们的数据库中

<?php

namespace App\Jobs;

use App\Registration;
use Cyberduck\LaravelExcel\ImporterFacade;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class BulkUploadJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $path;

    public function __construct($path)
    {
        $path = $path;
    }

    public function handle()
    {
        // Cyberduck Facade for Importing file from provided path
        $excel = ImporterFacade::make('Excel');
        // Loading  file from local public path
        $excel->load(public_path('uploads/' . $this->path));
        // storing data in a collection variable
        $collection = $excel->getCollection();
        $batch = random_int(1111, 9999);
        foreach ($collection  as $reg) {
            if (is_numeric($reg[0])) {
                $registration = Registration::find($reg[0]);
                if (isset($registration)) {
                    registration->firstname = $reg[1];
                    $registration->save();
                } else {
                    $registration = new Registration();
                    $registration->id = $reg[0];
                    $registration->firstname = $reg[1];
                    $registration->save();
                }
            }
        }
    }
}

4

1 回答 1

0

上述问题现已解决。

如果我们需要处理文件,那么我们需要直接将集合发送到队列,因为它无论如何都不会读取文件路径。

而不是来自控制器(BulkUploadController)的以下路径:

$file_path = public_path('uploads' . DIRECTORY_SEPARATOR . $fileName);

BulkUploadJob::dispatch($file_path)->delay(now()->addSeconds(10));

请直接将集合从控制器(BulkUploadController)发送到队列,如下所示:

$excel->load(public_path('uploads/' . $fileName)); 

$collection = $excel->getCollection(); 

BulkUploadJob::dispatch($collection)->delay(now()->addSeconds(10));

在我们的工作(BulkUploadJob)的处理方法中,如下所示:

public function handle()
    {
        $batch = random_int(1111, 9999);
        foreach ($this->collection as $collection) {
        //code to save the records in database
        }
    }

终于工作正常了:)

于 2020-06-22T21:06:50.953 回答