Laravel-excel: [BUG] Illuminate\Foundation\Bus\Dispatchable not found in Lumen 5.8.*

Created on 31 Aug 2019  路  4Comments  路  Source: Maatwebsite/Laravel-Excel

Prerequisites

  • [X] Checked if your Laravel Excel version is still supported: https://docs.laravel-excel.com/3.1/getting-started/support.html#supported-versions
  • [X] Able to reproduce the behaviour outside of your code, the problem is isolated to Laravel Excel.
  • [X] Checked that your issue isn't already filed.
  • [X] Checked if no PR was submitted that fixes this problem.
  • [X] Filled in the entire issue template

Versions

  • PHP version: 7.3.5
  • Laravel version: 5.8.12 (Lumen)
  • Package version: 3.1.15

Description


When using (new ResponsesExport)->queue('ttt.xlsx') I got an error that says

Fatal error: Trait 'Illuminate\Foundation\Bus\Dispatchable' not found in C:\wamp64\www\datacollection-backend\vendor\maatwebsite\excel\src\Jobs\AppendDataToSheet.php on line 13

Steps to Reproduce

Create a new Laravel Lumen project.
Add Laravel Excel as a dependence.
Dispatch a Job to queue using (new ResponsesExport)->queue('ttt.xlsx')

Expected behavior:
Dispatch the job, and the job got processed successfully

Actual behavior:
A Not found exception is thrown.

# Time Memory Function Location 1 0.4032 403696 {main}( ) ...\index.php:0 2 0.4433 1141024 Laravel\Lumen\Application->run( ) ...\index.php:28 3 0.4433 1141024 Laravel\Lumen\Application->dispatch( ) ...\RoutesRequests.php:108 4 0.4506 1379928 Laravel\Lumen\Application->sendThroughPipeline( ) ...\RoutesRequests.php:171 5 0.4506 1379928 Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}( ) ...\RoutesRequests.php:416 6 0.4506 1380304 Laravel\Lumen\Application->handleFoundRoute( ) ...\RoutesRequests.php:165 7 0.4507 1380304 Laravel\Lumen\Application->callActionOnArrayBasedRoute( ) ...\RoutesRequests.php:263 8 0.4509 1382896 Laravel\Lumen\Application->call( ) ...\RoutesRequests.php:289 9 0.4509 1382896 Illuminate\Container\BoundMethod::call( ) ...\Container.php:576 10 0.4509 1383592 Illuminate\Container\BoundMethod::callBoundMethod( ) ...\BoundMethod.php:34 11 0.4509 1383592 Illuminate\Container\BoundMethod::Illuminate\Container\{closure}( ) ...\BoundMethod.php:78 12 0.4613 1586688 call_user_func_array:{C:\wamp64\www\datacollection-backend\vendor\illuminate\container\BoundMethod.php:32} ( ) ...\BoundMethod.php:32 13 0.4613 1586704 Laravel\Lumen\Routing\Closure->{closure:C:\wamp64\www\datacollection-backend\routes\web.php:82-88}( ) ...\BoundMethod.php:32 14 0.4694 2120520 App\Exports\ResponsesExport2->queue( ) ...\web.php:84 15 0.4739 2121744 Maatwebsite\Excel\Excel->queue( ) ...\Exportable.php:81 16 0.4744 2122064 Maatwebsite\Excel\QueuedWriter->store( ) ...\Excel.php:123 17 0.4767 2123512 Maatwebsite\Excel\QueuedWriter->buildExportJobs( ) ...\QueuedWriter.php:64 18 0.4768 2123968 Maatwebsite\Excel\QueuedWriter->exportCollection( ) ...\QueuedWriter.php:93 19 0.4769 2125576 Illuminate\Support\Collection->map( ) ...\QueuedWriter.php:135 20 0.4769 2125952 array_map ( ) ...\Collection.php:1120 21 0.4769 2126048 Maatwebsite\Excel\QueuedWriter->Maatwebsite\Excel\{closure}( ) ...\Collection.php:1120 22 0.4773 2126424 spl_autoload_call ( ) ...\QueuedWriter.php:128 23 0.4773 2126504 Composer\Autoload\ClassLoader->loadClass( ) ...\QueuedWriter.php:128 24 0.4773 2126504 Composer\Autoload\includeFile( ) ...\ClassLoader.php:322 25 0.4785 2127480 include( 'C:\wamp64\www\datacollection-backend\vendor\maatwebsite\excel\src\Jobs\AppendDataToSheet.php' ) ...\ClassLoader.php:444

Additional Information

Any additional information, configuration or data that might be necessary to reproduce the issue.

proposal

Most helpful comment

The Dispatchable trait is not included in Lumen.

You can dispatch a job, without the the Dispatchable trait, by calling dispatch(new ResponsesExport);

See: https://lumen.laravel.com/docs/5.8/queues

All 4 comments

The Dispatchable trait is not included in Lumen.

You can dispatch a job, without the the Dispatchable trait, by calling dispatch(new ResponsesExport);

See: https://lumen.laravel.com/docs/5.8/queues

@d2x thanks for the solution. It worked for me.

But think there should be handling if Laravel's Dispatchable trait doesn't exists in application so that to not raise an exception in application, or catching the exception.

I have ran into this issue also when importing through my Lumen application.

To fix it I have removed the Dispatchable trait from \src\jobs\QueueImport.php

<?php
namespace Maatwebsite\Excel\Jobs;

use Illuminate\Contracts\Queue\ShouldQueue;

class QueueImport implements ShouldQueue
{
    use ExtendedQueueable;

    public function handle()
    {
        //
    }
}

And then changed the \src\ChunkReader.php at line 62

<?php

namespace Maatwebsite\Excel;

use Illuminate\Container\Container;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithLimit;
use Maatwebsite\Excel\Concerns\WithProgressBar;
use Maatwebsite\Excel\Events\BeforeImport;
use Maatwebsite\Excel\Files\TemporaryFile;
use Maatwebsite\Excel\Imports\HeadingRowExtractor;
use Maatwebsite\Excel\Jobs\AfterImportJob;
use Maatwebsite\Excel\Jobs\QueueImport;
use Maatwebsite\Excel\Jobs\ReadChunk;
use Throwable;

class ChunkReader
{
    /**
     * @param  WithChunkReading  $import
     * @param  Reader  $reader
     * @param  TemporaryFile  $temporaryFile
     *
     * @return \Illuminate\Foundation\Bus\PendingDispatch|null
     */
    public function read(WithChunkReading $import, Reader $reader, TemporaryFile $temporaryFile)
    {
        if ($import instanceof WithEvents && isset($import->registerEvents()[BeforeImport::class])) {
            $reader->beforeImport($import);
        }

        $chunkSize  = $import->chunkSize();
        $totalRows  = $reader->getTotalRows();
        $worksheets = $reader->getWorksheets($import);

        if ($import instanceof WithProgressBar) {
            $import->getConsoleOutput()->progressStart(array_sum($totalRows));
        }

        $jobs = new Collection();
        foreach ($worksheets as $name => $sheetImport) {
            $startRow         = HeadingRowExtractor::determineStartRow($sheetImport);
            $totalRows[$name] = $sheetImport instanceof WithLimit ? $sheetImport->limit() : $totalRows[$name];

            for ($currentRow = $startRow; $currentRow <= $totalRows[$name]; $currentRow += $chunkSize) {
                $jobs->push(new ReadChunk(
                    $import,
                    $reader->getPhpSpreadsheetReader(),
                    $temporaryFile,
                    $name,
                    $sheetImport,
                    $currentRow,
                    $chunkSize
                ));
            }
        }

        $jobs->push(new AfterImportJob($import, $reader));

        if ($import instanceof ShouldQueue) {
            return Container::getInstance()
                ->make(Dispatcher::class)
                ->dispatch((new QueueImport)->chain($jobs->toArray()));
        }

        $jobs->each(function ($job) {
            try {
                dispatch_now($job);
            } catch (Throwable $e) {
                if (method_exists($job, 'failed')) {
                    $job->failed($e);
                }
                throw $e;
            }
        });

        if ($import instanceof WithProgressBar) {
            $import->getConsoleOutput()->progressFinish();
        }

        unset($jobs);

        return null;
    }
}

Ideally this should be fixed throughout to not require Dispatchable though of course.

As I've mentioned a few times in other tickets, we don't support Lumen at the moment. It probably works for most of the cases, but we never used it ourselves. I'll changes this into a proposal and will see if we can support Lumen better in future versions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

octoxan picture octoxan  路  3Comments

lucatamtam picture lucatamtam  路  3Comments

rossjcooper picture rossjcooper  路  3Comments

gamevnlc picture gamevnlc  路  3Comments

muhghazaliakbar picture muhghazaliakbar  路  3Comments