0

I am getting an exception while implementing a queue (Jobs) for reading data & store in the database from an excel sheet (cyber-duck/laravel-excel package), the file is already stored in my computer.

An Exception is as below:

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}

Controller (BulkUploadController), it is used to upload the file in a place from where we can get the collection of data from that file and send for dispatching the job

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), it used to process the excel file data into our database

<?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();
                }
            }
        }
    }
}

ashish patel
  • 169
  • 1
  • 6

1 Answers1

0

The above problem has been resolved now.

If we need to process the file then we need to send the collection to the queue directly because it will not read the file path anyhow.

Instead of below path from controller (BulkUploadController):

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

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

Pleas send the collection directly from controller (BulkUploadController) to the queue as below:

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

$collection = $excel->getCollection(); 

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

In handle method of our job (BulkUploadJob) will be like below:

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

Finally The Job is Working Fine :)

ashish patel
  • 169
  • 1
  • 6