Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

wonder95's avatar

Unable to get pdo connection after update to Laravel 10 and MongoDb\Laravel

I work on a huge Laravel/Vue/MongoDb app, and I have been in the process of upgrading to Laravel 10, along with the change from Jenssegers\Mongodb to MongoDb\Laravel, along with a cartalyst/stripe-billing and I got all the associate libraries updated and installed, but when I now attempt to connect to the database (or run migrations during test runs), I get an error at this code in the Illuminate Connection.php

$statement = $this->prepared(
                $this->getPdoForSelect($useReadPdo)->prepare($query)
            );

which goes to

protected function getPdoForSelect($useReadPdo = true)
    {
        return $useReadPdo ? $this->getReadPdo() : $this->getPdo();
    }

and then to

public function getPdo()
    {
        if ($this->pdo instanceof Closure) {
            return $this->pdo = call_user_func($this->pdo);
        }

        return $this->pdo;
    }

and since $this->pdo is null, it throws an error.

Nothing has changed on our end. However, I do notice that in the constructor for the MongoDb\Laravel version of Connect.php. there is no $this->pdo set:

    public function __construct(array $config)
    {
        $this->config = $config;

        // Build the connection string
        $dsn = $this->getDsn($config);

        // You can pass options directly to the MongoDB constructor
        $options = $config['options'] ?? [];

        // Create the connection
        $this->connection = $this->createConnection($dsn, $config, $options);

        // Select database
        $this->db = $this->connection->selectDatabase($this->getDefaultDatabaseName($dsn, $config));

        $this->useDefaultPostProcessor();

        $this->useDefaultSchemaGrammar();

        $this->useDefaultQueryGrammar();
    }

vs the overridden Illuminate version

    public function __construct($pdo, $database = '', $tablePrefix = '', array $config = [])
    {
        $this->pdo = $pdo;

        // First we will setup the default properties. We keep track of the DB
        // name we are connected to since it is needed when some reflective
        // type commands are run such as checking whether a table exists.
        $this->database = $database;

        $this->tablePrefix = $tablePrefix;

        $this->config = $config;

        // We need to initialize a query grammar and the query post processors
        // which are both very important parts of the database abstractions
        // so we initialize these to their default values while starting.
        $this->useDefaultQueryGrammar();

        $this->useDefaultPostProcessor();
    }

hence the error. Do I have to define my own $this->pdo somewhere else?

0 likes
7 replies
LaryAI's avatar
Level 58

It seems like you're encountering an issue with the MongoDB connection in Laravel 10 after switching from jenssegers/mongodb to mongodb/laravel. The error you're experiencing is due to the fact that the MongoDB connection does not use the traditional PDO (PHP Data Objects) that is used for SQL databases. Instead, MongoDB uses its own connection class and methods provided by the MongoDB PHP library.

The MongoDB\Laravel package likely has its own connection class that does not have a $pdo property because it doesn't use PDO for connecting to the MongoDB server. This is why you're seeing null when trying to access $this->pdo.

Here's what you can do to resolve the issue:

  1. Ensure that you have properly configured the MongoDB connection in your config/database.php file. It should look something like this:
'mongodb' => [
    'driver' => 'mongodb',
    'host' => env('DB_HOST', 'localhost'),
    'port' => env('DB_PORT', 27017),
    'database' => env('DB_DATABASE'),
    'username' => env('DB_USERNAME'),
    'password' => env('DB_PASSWORD'),
    'options' => [
        'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'), // required with Mongo 3+
    ],
],
  1. Make sure you are using the correct connection when running migrations or querying the database. For example, when running migrations, you can specify the connection:
Schema::connection('mongodb')->create('users', function (Blueprint $collection) {
    // ...
});
  1. If you have custom code that relies on $this->pdo, you will need to refactor it to use the MongoDB connection methods instead. The MongoDB connection does not have a getPdo() method because it is not applicable.

  2. If you are extending or overriding any of the core Laravel database classes, ensure that you are doing so in a way that is compatible with the MongoDB connection. You may need to create custom implementations that do not rely on PDO.

  3. Check the documentation of the mongodb/laravel package for any additional steps required for the upgrade or for compatibility with Laravel 10.

If you continue to have issues, it might be helpful to reach out to the maintainers of the mongodb/laravel package for support or to check if there are any known issues with Laravel 10 compatibility.

wonder95's avatar

Well, all I"m doing is a simple call to an Eloquent relationship (getting Stripe subscriptions using Cartalyst's Stripe Billing), which is what kicks off the whole thing, and that's the way we've been doing them all along. Like I said, nothing has changed on our end.

jlrdw's avatar

@wonder95 are you using the same php version as before?

What is the exact error?

wonder95's avatar

Minimum PHP version per the docs is 8.1 now. I have been using 8.2 for a while, so that hasn't changed.

The error is

Call to a member function prepare() on null at /Users/sedwards/Sites/govtribe/web/vendor/laravel/framework/src/Illuminate/Database/Connection.php:416

where that line is

$this->getPdoForSelect($useReadPdo)->prepare($query)

in the first code block above.

wonder95's avatar

All of our models are already exetnding the correct model, e.g.:

use MongoDB\Laravel\Eloquent\Model;
...
abstract class EntityModel extends Model
wonder95's avatar
wonder95
OP
Best Answer
Level 5

So it turned out that there was another buried model in a library that was being used that did not extend the MongoDb\Laravel model. Once that was fixed the problem disappeared.

Please or to participate in this conversation.