-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Unsupported driver [mongodb] #2643
-
I'm currently working with PHP (v8.1), Laravel/Lumen Framework (^10.0), MongoDB (v7.0.2), mongodb/laravel-mongodb(^4.0), mongodb/mongodb(^1.16) and Linux (v20.04). I've successfully installed the PHP MongoDB extension. I verified its installation for the CLI using the command php -m | grep mongodb
, which returned "mongodb". Additionally, through phpinfo()
, I confirmed that the MongoDB extension is enabled in the Apache web server.
For the Laravel-Lumen setup, I've learned that one should register MongoDB\Laravel\MongoDBServiceProvider::class
before invoking $app->withEloquent()
. However, when I attempt this, I encounter an error: "Call to a member function prepare() on null". Furthermore, a recurrent error I face states "Unsupported driver [mongodb]". I'm puzzled by these issues. Can someone shed some light on them and assist me?
bootstrap/app.php
<?php
require_once __DIR__.'/../vendor/autoload.php';
(new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
dirname(__DIR__)
))->bootstrap();
date_default_timezone_set(env('APP_TIMEZONE', 'UTC'));
$app = new Laravel\Lumen\Application(
dirname(__DIR__)
);
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
$app->configure('jwt');
$app->configure('api');
$app->configure('database');
$app->withFacades();
$app->withEloquent();
$app->middleware([
]);
$app->routeMiddleware([
]);
$app->register(App\Providers\AuthServiceProvider::class);
$app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class);
$app->register(App\Providers\DatabaseServiceProvider::class);
$app->register(MongoDB\Laravel\MongoDBServiceProvider::class);
$app->router->group([
'namespace' => 'App\Http\Controllers',
], function ($router) {
require __DIR__.'/../routes/web.php';
});
return $app;
Beta Was this translation helpful? Give feedback.
All reactions
I found the cause of the problem.
See this Provider:
If there is a package run "app('db')" or "app->make('db)" in its provider, before this code in laravel-mongodb package.
Because db is registered as a singleton,so the resolving function will not be executed again.
So mongodb cannot be properly bound to the Provider.
Replies: 1 comment 6 replies
-
same bug, need fix
Beta Was this translation helpful? Give feedback.
All reactions
-
I found the cause of the problem.
See this Provider:
image
If there is a package run "app('db')" or "app->make('db)" in its provider, before this code in laravel-mongodb package.
Because db is registered as a singleton,so the resolving function will not be executed again.
So mongodb cannot be properly bound to the Provider.
Beta Was this translation helpful? Give feedback.
All reactions
-
In the final analysis, it is because mongo is registered through resolving rather than through the make function.
This may be a BUG, but it's not clear why need to regist by resolving instead of make.
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi, thanks for finding this - do you have any suggestions about how to resolve it?
Beta Was this translation helpful? Give feedback.
All reactions
-
I've explained the issue here: #2715 (comment)
Beta Was this translation helpful? Give feedback.
All reactions
-
I temporarily solved the problem by modifying the loading order of providers in config/app.php, but this is only a temporary solution.
@spinalwiz
Here example:
'providers' => ServiceProvider::defaultProviders()->merge([
// Ensure that conflicting packages load first
Mnabialek\LaravelSqlLogger\Providers\ServiceProvider::class,
// Application Service Providers
App\Providers\AppServiceProvider::class
// ...etc
])->toArray(),
Beta Was this translation helpful? Give feedback.
All reactions
-
👎 1