designmynight / laravel-mongodb-passport

A package to get Laravel Passport working with MongoDB
MIT License
67 stars 75 forks source link

Call to a member function beginTransaction() on null in file laravel/framework/src/Illuminate/Database/Concerns/ManagesTransactions.php on line 108 #15

Closed koAndy closed 5 years ago

koAndy commented 5 years ago

I'm using Laravel 5.6 and Passport 7.0 with Jenssengers MongoDB. Installation of passport when very well, including migrations. Every collection exists. I have two records in oauth_clients collections. I have created a new user in users collection.

Trying to login my user through oauth/token, generate this error: Call to a member function beginTransaction() on null in file laravel/framework/src/Illuminate/Database/Concerns/ManagesTransactions.php on line 108

Any advice?

StevePorter92 commented 5 years ago

Hey @koAndy, do you have a full stack trace?

StevePorter92 commented 5 years ago

From memory, I think I've seen that error when using the RefreshDatabase trait when testing. And just from having a quick scan, it looks like there are only a few places that you might receive that error. Are you using the database as your session driver?

koAndy commented 5 years ago

Hi, thanks for quick answer. I do not use any testing. Just want to create an API and stated with login using Laravel Passport.

First, when tried the setup, I had this error on any request made in api.php but removing throttle from app/Http\Kernel.php ('throttle:60,1') made things work nicely.

Now the error appears when a request is made on http://localhost/oauth/token, even without any parameters sent to POST request with POSTMAN.

The stacktrace is this:

Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function beginTransaction() on null in file /Users/user/PHP-Projects/myproject/vendor/laravel/framework/src/Illuminate/Database/Concerns/ManagesTransactions.php on line 108 Stack trace:

  1. Symfony\Component\Debug\Exception\FatalThrowableError->() /Users/user/PHP-Projects/myproject/vendor/laravel/framework/src/Illuminate/Database/Concerns/ManagesTransactions.php:108
  2. {main}() /Users/user/PHP-Projects/myproject/server.php:0

Maybe oauth has some throttling, too?

And, yes, the sessions are in database too, but switching back to 'file' results in same error mentioned above.

StevePorter92 commented 5 years ago

Does your User class extend DesignMyNight\Mongodb\Auth\User? It sounds like you have something that isn't routing your code your code properlty. Please double check you have followed the instructions in

https://github.com/jenssegers/laravel-mongodb#installation https://github.com/jenssegers/laravel-mongodb#configuration https://github.com/jenssegers/laravel-mongodb#configuration https://laravel.com/docs/master/passport#installation

If your using https://github.com/jenssegers/laravel-mongodb, the database session driver isn't supported without this package https://github.com/jenssegers/Laravel-MongoDB-Session, in the meantime i would suggest reverting to the file driver.

koAndy commented 5 years ago

I have managed to make it work.

Removed middleware throttle from routeRegistrar.php from vendor/Laravel/passport made the trick.

Session seems to not have any problem.

Thanks!

StevePorter92 commented 5 years ago

Glad you managed to fix it! Out of curiosity, are you using the database as your cache driver?

koAndy commented 5 years ago

Yes, cache driver in database also. And guess what? I have put the throttle back in Kernel and RouteRegistrar as originally was, and put CACHE_DRIVER in file mode and everything works as it should.

So it seems that CACHE_DRIVER=database causes the problem.

Thanks a lot for the tip!

Do you have any advice to make it work with CACHE_DRIVER in database mode and without removing throttle from those two files?

StevePorter92 commented 5 years ago

in config/app.php

Try setting the connection to mongodb under database EDIT

 'stores' => [

        'apc' => [
            'driver' => 'apc',
        ],

        'array' => [
            'driver' => 'array',
        ],

        'database' => [
            'driver' => 'database', 
            'table' => 'cache',
            'connection' => 'mongodb', <--------
        ],
koAndy commented 5 years ago

I think you wanted to say config/cache.php Anyway not working. After setting mongodb returns: "Driver [mongodb] is not supported."

StevePorter92 commented 5 years ago

Sorry, yes i meant config/cache.php, but also meant connection, not driver.

koAndy commented 5 years ago

Nop, changing connection from null to mongodb returns Call to a member function beginTransaction() on null as stated at the begging of this issue.

StevePorter92 commented 5 years ago

Sounds like https://github.com/jenssegers/laravel-mongodb doesn't work as a database cache driver then. Sorry, i'm out of ideas without looking into it further.

koAndy commented 5 years ago

Ok, staying on file CACHE_DRIVER for the moment. Thanks!