mnabialek / laravel-sql-logger

Log SQL queries for Laravel/Lumen framework
MIT License
156 stars 24 forks source link

Register the DB listener through the boot function instead of register function #37

Open zyz954489346 opened 8 months ago

zyz954489346 commented 8 months ago

All service providers extend the Illuminate\Support\ServiceProvider class. Most service providers contain a register and a boot method. Within the register method, you should only bind things into the service container. You should never attempt to register any event listeners, routes, or any other piece of functionality within the register method.

As stated in the laravel document.The registration of listener should not be placed on register. This is the current provider code:

    public function register()    
    {    
        // ...    
        // listen to database queries    
        $this->app['db']->listen($this->getListenClosure($logger));    
    }    

$this->app['db'] will cause db to be instantiated,Any provider executed after that, if it contains changes to db, will be ignore because the db is instantiated in advance.

There is a bug, which causes the official package of laravel-mongodb not to be correctly register in the resolving callback of db. This bug is modified by the official suggestion of mongodb. see https://github.com/mongodb/laravel-mongodb/issues/2715#issue-2104526953

So the solution to the problem is that db should not be instantiated in register, which does not conform to the specification of laravel and can lead to conflicts between packages.

r0bdiabl0 commented 1 week ago

This caused me a headache for 3 days. I had to slowly remove packages from composer.json to discover this package was the issue. I use Laravel 11 and MongoDB Laravel and this package would prevent me from adding MongoDB as a new connection. Please update!