我正在尝试在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)
我在这里做什么错了?
答案 0 :(得分:0)
好的,答案实际上很简单,但很令人沮丧。
我正在将valet
与Laravel结合使用,并且正在访问我的站点,网址为:mywebsite.test
。我尝试通过以下方式加载它:
php artisan serve
,然后尝试从IP(127.0.0.1
)进行完全相同的操作-然后它起作用了!
所以我刚刚重启了代客服务,然后在那儿也能正常工作。
valet restart
这很奏效。