Laravel-mongodb: Error when user registration -> Call to a member function prepare() on null

Created on 23 Oct 2017  路  23Comments  路  Source: jenssegers/laravel-mongodb

Dear programmers, I just using this package for my project
I have done configurations in this readme file.

But when I use Laravel default Auth, and user want to register, it says :

"Call to a member function prepare() on null"
"at Connection->IlluminateDatabase{closure}('insert into "users" ("name", "email", "password", "updated_at", "created_at") values (?, ?, ?, ?, ?)', array(.............."
"in Connection.php (line 640)"

Please help me how to solve it

Most helpful comment

I seem to have found a fix. The default User model is extending IlluminateDatabaseEloquentModel, where it has to extend JenssegersMongodbEloquentModel to work with MongoDB. By changing the User model in app/User.php, this can be resolved. Replace app/User.php with the code from the following gist:

https://gist.github.com/bramnauta/513ca76f4ff7811583fe91372ce6ddab

All 23 comments

I'm having the exact same issue. I'm using Laravel 5.5 and laravel-mongodb 3.3.0-alpha.

In my case, the error occurs in vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 452; it seems $this->getPdo() returns null. The error occurs in the code below.

public function statement($query, $bindings = [])
{

return $this->run($query, $bindings, function ($query, $bindings) {
    if ($this->pretending()) {
        return true;
    }

    $statement = $this->getPdo()->prepare($query);  // Error occurs in this line

    $this->bindValues($statement, $this->prepareBindings($bindings));

    $this->recordsHaveBeenModified();

    return $statement->execute();
});

}

Same as @bramnauta here :(

I seem to have found a fix. The default User model is extending IlluminateDatabaseEloquentModel, where it has to extend JenssegersMongodbEloquentModel to work with MongoDB. By changing the User model in app/User.php, this can be resolved. Replace app/User.php with the code from the following gist:

https://gist.github.com/bramnauta/513ca76f4ff7811583fe91372ce6ddab

Just found this and it's works for me https://github.com/jenssegers/laravel-mongodb/issues/1181

@Rudy-vgs That does the same thing; I didn't know there was a JenssegersMongodbAuthUser class :). That's a lot cleaner!

this did not work for me. Any other suggestions?

same issue here, encounterd while trying to "belongsTo" a mysql relation

@rachelies I was facing the same issue and figured out how to fix.

I have the following entities:
SearchRequestLog (mongodb connection)
Marketplace (mysql connection)

But for some reason some properties from Marketplace were being overridden and one of them was $connection. So I had to force and set it manually with the following result:

class Marketplace extends \Illuminate\Database\Eloquent\Model
{
    protected $connection = 'mysql';
}

Tables are created but on trying to create users it fails > Laravel 5.7, latest laravel-mongodb, using the JenssegersMongodbAuthUser as Authenticatable method. It's clear that Laravel is looking for a pdo instance and not finding it because the Jenssegers Connection bypasses the Laravel Connection constructor, so pdo is never set. I understand why pdo isn't used, but it's blowing up any attempt at preparing a statement.

```php artisan passport:install

SymfonyComponentDebugExceptionFatalThrowableError : Call to a member function prepare() on null

at ... /vendor/laravel/framework/src/Illuminate/Database/Connection.php:452
448| if ($this->pretending()) {
449| return true;
450| }
451|

452| $statement = $this->getPdo()->prepare($query);
453|
454| $this->bindValues($statement, $this->prepareBindings($bindings));
455|
456| $this->recordsHaveBeenModified();

Exception trace:

1 IlluminateDatabaseConnection::IlluminateDatabase{closure}("insert into "oauth_clients" ("user_id", "name", "secret", "redirect", "personal_access_client", "password_client", "revoked", "updated_at", "created_at") values (?, ?, ?, ?, ?, ?, ?, ?, ?)")
... /vendor/laravel/framework/src/Illuminate/Database/Connection.php:657

2 IlluminateDatabaseConnection::runQueryCallback("insert into "oauth_clients" ("user_id", "name", "secret", "redirect", "personal_access_client", "password_client", "revoked", "updated_at", "created_at") values (?, ?, ?, ?, ?, ?, ?, ?, ?)", Object(Closure))
/Users/logan/Documents/Projects/maglev-domain/vendor/laravel/framework/src/Illuminate/Database/Connection.php:624

Please use the argument -v to see more details.```

@rakmaster I also have exact same issue. did you solve it ?

same problem here, any ideas?

Just replace the IlluminateDatabaseEloquentModel; with JenssegersMongodbEloquentModel
in below files
..vendorlaravelpassportsrcToken.php
..vendorlaravelpassportsrcAuthCode.php
..vendorlaravelpassportsrcClient.php
..vendorlaravelpassportsrcPersonalAccessClient.php

and protected $table= "Table_name" variable with protected $collection="Table_name"

As we passport by default takes the mysql PDO connection but at backed we have MongoDB.

Just replace the IlluminateDatabaseEloquentModel; with JenssegersMongodbEloquentModel
in below files
..vendorlaravelpassportsrcToken.php
..vendorlaravelpassportsrcAuthCode.php
..vendorlaravelpassportsrcClient.php
..vendorlaravelpassportsrcPersonalAccessClient.php

and protected $table= "Table_name" variable with protected $collection="Table_name"

As we passport by default takes the mysql PDO connection but at backed we have MongoDB.

This solved my problem, thanks!

Same problem @rakmaster , did you solve it?

@dizzyboy95 : but these files will overrride when you do composer update..
Is this permanant solution?

@sjachak, you can create another files and replaces paths in service provider

@adnanfajr, this issue is actual for you?

@adnanfajr, this issue is actual for you?

yes

For newer versions of passport, to override for MongoDB just do https://laravel.com/docs/7.x/passport#overriding-default-models

Though hilariously this method doesn't actually work within passport itself because it uses DI to enforce models only coming from the core package when running commands like install

@bramnauta,your code works for me very well. Keep Going

none of the solutions here worked for me on laravel 8

Was this page helpful?
0 / 5 - 0 ratings

Related issues

viacheslavpleshkov picture viacheslavpleshkov  路  3Comments

yupangestu picture yupangestu  路  3Comments

ricardofontanelli picture ricardofontanelli  路  3Comments

sanjay1688 picture sanjay1688  路  3Comments

naveedyasin picture naveedyasin  路  3Comments