【问题标题】:Class 'Jenssegers\Mongodb\Eloquent\Model' not found找不到类“Jenssegers\Mongodb\Eloquent\Model”
【发布时间】:2020-06-17 18:53:29
【问题描述】:

Laravel 版本: 7.16.1
PHP 版本: 7.4
数据库驱动和版本: "jenssegers/mongodb": "4.0.0-alpha.1"

我有一个从控制台调用的队列应用程序。当我想插入集合时,我遇到了错误。原因是什么?我认为是错误。

> Class 'Jenssegers\Mongodb\Eloquent\Model' not found
> {"exception":"[object] (Error(code: 0): Class
> 'Jenssegers\Mongodb\Eloquent\Model' not found

当我在浏览器上正常使用时。没关系。

我做了“composer dump-autoload”但没有改变任何东西。

我的结构:

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        'Illuminate\Auth\Events\Logout' => [
            'App\Listeners\LogSuccessfulLogout',
        ],
        'eloquent.created: *' => [
            'App\Listeners\EloquentListener',
        ],
        'eloquent.updated: *' => [
            'App\Listeners\EloquentListener',
        ],
        'eloquent.deleted: *' => [
            'App\Listeners\EloquentListener',
        ],
        'eloquent.restored: *' => [
            'App\Listeners\EloquentListener',
        ]
    ];



<?php

namespace App\Listeners;

use App\Http\Controllers\Common\Models\ActivityLogs;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class EloquentListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  Logout  $event
     * @return void
     */
    public function handle($event, $data)
    {
        $disableFire = [
            'ActivityLogs'
        ];

        $continue = true;
        foreach ($disableFire as $item) {
            $continue = strstr($event, $item) ? false: true;
        }

        if ($continue) {
            ActivityLogs::create([
                'fire_event' => $event,
                'user_id' => auth()->id(),
                'site_id' => auth()->user()->site_id
            ]);
        }

        return true;

    }

}

<?php

namespace App\Http\Controllers\Common\Models;

use Jenssegers\Mongodb\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\SoftDeletes;

class ActivityLogs extends Model {

    use SoftDeletes;

    protected $dates = ['created_at', 'updated_at', 'deleted_at'];

    protected $collection = 'activity_logs';

    protected $connection = 'mongodb';

    protected $fillable = [
        //'activity_log_id',
        'fire_event',
        'site_id',
        'user_id',
        'deleted_at',
        'created_at',
        'updated_at'
    ];
}

【问题讨论】:

  • 尝试重新启动队列?每当您对代码库进行任何更改时,都需要重新启动它。

标签: laravel mongodb laravel-7.x laravel-mongodb


【解决方案1】:

你在config中配置了queue.php吗?

https://github.com/jenssegers/laravel-mongodb#queues

您是否配置了 .env 文件?

你安装包了吗?

composer 需要 jenssegers/mongodb

这是一个示例教程的链接

https://appdividend.com/2018/05/10/laravel-mongodb-crud-tutorial-with-example/

【讨论】:

  • 很遗憾,它与队列无关。我正在使用带有队列的redis。我在事件监听器中使用 mongodb。当我在浏览器上使用时,没关系。我可以对模型进行 CRUD。但是当我在 CLI 上使用它时,找不到 Class 'Jenssegers\Mongodb\Eloquent\Model'
  • 啊好吧,我看到它是 4.0.0
  • 使用混合关系;也适用于第一个关系模型。另一个建议是在 mysql 和 MongoDB 模型类中添加连接名称,即使它们有关系,即使它们有默认连接名称,如果在某些情况下不这样做会导致问题。
  • 这是一个redis与MongoDb一起使用的教程epsagon.com/blog/development/…
  • 谢谢,但这与我的问题无关。
猜你喜欢
  • 1970-01-01
  • 2020-10-09
  • 2017-12-06
  • 2018-02-25
  • 2016-07-26
  • 1970-01-01
  • 2020-02-02
  • 2017-05-21
  • 1970-01-01
相关资源
最近更新 更多