我正在尝试在 Laravel 中使用队列,并为此安装了 Redis 和 Horizon。
我的用户可以通过前端上传图片。发生这种情况时,它会调用 store
方法:
public function store(Stream $stream)
{
//Validate the request.
$validate = request()->validate([
'file' => 'mimes:jpeg,jpg,bmp,png,gif,pdf',
]);
ImportDocuments::dispatch($stream);
}
在我的 Jobs/ImportDocuments.php
类中,我有这段代码:
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);
}
}
供您引用,addDocuments()
方法如下所示:
Stream.php
:
public function addDocuments(array $attributes)
{
return $this->documents()->create($attributes);
}
每当我尝试上传图片时,都会出现以下错误:
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 正在运行
- predis/predis 已安装
- 地平线正在运行
- Horizon 状态为“有效”
我在这里做错了什么?
最佳答案
好吧,答案实际上很简单,但非常令人沮丧。
我在 Laravel 中使用 valet
,并且正在访问我的站点:mywebsite.test
。我试图通过加载它:
php artisan serve
,然后尝试完全相同的操作,只是从 IP (127.0.0.1
) - 然后成功了!
所以我只是重新启动了 valet,然后它也在那里工作了。
valet restart
砰,它成功了..
关于PHP - 作业/队列 - 类分派(dispatch)不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54990964/