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()
{
//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":"myemail@myapp.com","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?