PHP - 作业/队列 - 类分派(dispatch)不存在

标签 php laravel redis

我正在尝试在 Laravel 中使用队列,并为此安装了 Redis 和 Horizo​​n。

我的用户可以通过前端上传图片。发生这种情况时,它会调用 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 已安装
  • 地平线正在运行
  • Horizo​​n 状态为“有效”

我在这里做错了什么?

最佳答案

好吧,答案实际上很简单,但非常令人沮丧。

我在 Laravel 中使用 valet,并且正在访问我的站点:mywebsite.test。我试图通过加载它:

php artisan serve,然后尝试完全相同的操作,只是从 IP (127.0.0.1) - 然后成功了!

所以我只是重新启动了 valet,然后它也在那里工作了。

valet restart

砰,它成功了..

关于PHP - 作业/队列 - 类分派(dispatch)不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54990964/

相关文章:

php - 解析 SQL 字符串

php - 解析巨大的GeoJSON文件并获取特定属性的多边形坐标

php - laravel背包用户密码

php - 过滤多对多关系 - Laravel

node.js - 如何停止以 --daemonize yes 启动的 redis 服务器

python - 使用 Python 和 Redis 进行优雅的缓存

PHP: preg_match 长度限制

php - 使用等号从数据库中获取值,同时忽略某些字母

javascript - 使用 Dropzone 'ProcessQueue' 事件发送参数

python - 为应用程序编写单元测试的模拟 redis