Laravel Queues - Class dispatch does not exist

Posted 1 month ago by oliverbusk

I am trying to use queues in Laravel, and have installed Redis and Horizon for this purpose.

My users can upload images through a frontend. When this happens, it calls a store method:

public function store(Stream $stream)
{
    //Validate the request.
    $validate = request()->validate([
        'file' => 'mimes:jpeg,jpg,bmp,png,gif,pdf',
    ]);

    ImportDocuments::dispatch($stream);
}

In my Jobs/ImportDocuments.php class, I have this code:

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

    protected $stream;

    /**
     * Create a new job instance.
     *
     * @param  Document  $document
     * @return void
     */
    public function __construct(Stream $stream)
    {
        $this->stream = $stream;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(Request $request)
    {
        //Store the actual document. returns the path.
        $store = $request->file('file')->store($this->stream->token);

        //Set the attributes to save to DB.
        $attributes['name'] = $request->file->getClientOriginalName();
        $attributes['path'] = $store;

        //Add the document to the database.
        $this->stream->addDocuments($attributes);
    }
}

For your reference, the addDocuments() method look like this:

Stream.php:

public function addDocuments(array $attributes)
{
    return $this->documents()->create($attributes);
}

Whenever I try to upload an image, I get below error:

Class dispatch does not exist {"userId":1,"email":"[email protected]","exception":"[object] (ReflectionException(code: -1): Class dispatch does not exist at /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Container/Container.php:779)
  • Redis is running
  • predis/predis is installed
  • Horizon is running
  • Horizon status says 'Active'

What am I doing wrong here?

Edit:

This is the full [stacktrace]:

[stacktrace]
#0 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Container/Container.php(779): ReflectionClass->__construct('dispatch')
#1 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Container/Container.php(658): Illuminate\Container\Container->build('dispatch')
#2 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Container/Container.php(609): Illuminate\Container\Container->resolve('dispatch', Array)
#3 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(759): Illuminate\Container\Container->make('dispatch', Array)
#4 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Container/Container.php(1222): Illuminate\Foundation\Application->make('dispatch')
#5 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Queue/SyncQueue.php(102): Illuminate\Container\Container->offsetGet('dispatch')
#6 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Queue/SyncQueue.php(117): Illuminate\Queue\SyncQueue->raiseExceptionOccurredJobEvent(Object(Illuminate\Queue\Jobs\SyncJob), Object(ReflectionException))
#7 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Queue/SyncQueue.php(46): Illuminate\Queue\SyncQueue->handleException(Object(Illuminate\Queue\Jobs\SyncJob), Object(ReflectionException))
#8 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(184): Illuminate\Queue\SyncQueue->push(Object(App\Jobs\ImportDocuments))
#9 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(160): Illuminate\Bus\Dispatcher->pushCommandToQueue(Object(Illuminate\Queue\SyncQueue), Object(App\Jobs\ImportDocuments))
#10 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(73): Illuminate\Bus\Dispatcher->dispatchToQueue(Object(App\Jobs\ImportDocuments))
#11 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Foundation/Bus/PendingDispatch.php(112): Illuminate\Bus\Dispatcher->dispatch(Object(App\Jobs\ImportDocuments))
#12 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Foundation/Bus/Dispatchable.php(16): Illuminate\Foundation\Bus\PendingDispatch->__destruct()
#13 /Users/MyUsername/Sites/playground/app/Http/Controllers/Stream/StreamDocumentsController.php(44): App\Jobs\ImportDocuments::dispatch(Object(App\Stream))
#14 [internal function]: App\Http\Controllers\Stream\StreamDocumentsController->store(Object(App\Stream))
#15 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array)
#16 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\Routing\Controller->callAction('store', Array)
#17 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Route.php(219): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\Stream\StreamDocumentsController), 'store')
#18 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Route.php(176): Illuminate\Routing\Route->runController()
#19 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Router.php(682): Illuminate\Routing\Route->run()
#20 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#21 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authorize.php(45): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#22 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\Auth\Middleware\Authorize->handle(Object(Illuminate\Http\Request), Object(Closure), 'create', 'App\\Stream')
#23 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#24 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#25 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
#26 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#27 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(43): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#28 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\Auth\Middleware\Authenticate->handle(Object(Illuminate\Http\Request), Object(Closure))
#29 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#30 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(75): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#31 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#32 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#33 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#34 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#35 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#36 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(63): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#37 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#38 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#39 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#40 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#41 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#42 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(66): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#43 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#44 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#45 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#46 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Router.php(684): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#47 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Router.php(659): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#48 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Router.php(625): Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route))
#49 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Router.php(614): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#50 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#51 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#52 /Users/MyUsername/Sites/playground/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#53 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Fideloper\Proxy\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure))
#54 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#55 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(31): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#56 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#57 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#58 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(31): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#59 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#60 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#61 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#62 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#63 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#64 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(62): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#65 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#66 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#67 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#68 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#69 /Users/MyUsername/Sites/playground/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#70 /Users/MyUsername/Sites/playground/public/index.php(55): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#71 /Users/MyUsername/.composer/vendor/laravel/valet/server.php(158): require('/Users/oliverbu...')
#72 {main}
"}

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.