laravel-mongodb icon indicating copy to clipboard operation
laravel-mongodb copied to clipboard

DB::enableQueryLog not working when using DatabaseMigrations

Open btaskew opened this issue 3 months ago • 0 comments

  • Laravel-mongodb Version: 5.5.0
  • PHP Version: 8.4.4
  • Database Driver & Version: mongodb/mongodb 2.1.1

Description:

Using DB::enableQueryLog within a test that is using the DatabaseMigrations trait fails with an error Call to a member function addSubscriber() on null

The laravel-mongodb version of enableQueryLog is trying to access the connection property on the Connection class (see here). However since Laravel 12.22 the connection is disconnected after the database wipe, as part of the db migration within DatabaseMigrations, so the connection property is null. See Laravel PR 56368

Steps to reproduce

  1. Create unit test (PHPUnit or Pest) using DatabaseMigrations trait
  2. Add DB::enableQueryLog, either within the test or within the code
  3. Run the test

Expected behaviour

Query log should be enabled, and results should be visible from DB::getQueryLog

Actual behaviour

Test errors

Error: Call to a member function addSubscriber() on null
at vendor/mongodb/laravel-mongodb/src/Connection.php:186
at vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:489
at vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:363

btaskew avatar Sep 11 '25 08:09 btaskew