Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default "SwitchTenantDatabaseTask" breaks mongodb/laravel-mongodb "mongodb" driver registration #576

Open
badger-adam opened this issue Jan 17, 2025 · 0 comments

Comments

@badger-adam
Copy link

The default SwitchTenantDatabaseTask instantiates the 'db' app container too early causing other extensions to 'db' that are properly being registered to break if loaded afterwards.

app('db')->extend($tenantConnectionName, function ($config, $name) use ($databaseName) {
    $config['database'] = $databaseName;

    return app('db.factory')->make($config, $name);
});

The issue is honestly better described here:
mongodb/laravel-mongodb#2715 (comment)

To fix this code, we should call it in a 'resolving' call.

app()->resolving('db', function ($db) use ($tenantConnectionName, $databaseName) {
    $db->extend($tenantConnectionName, function ($config, $name) use ($databaseName) {
        $config['database'] = $databaseName;

        return app('db.factory')->make($config, $name);
    });
});
@badger-adam badger-adam changed the title Default "SwitchTenantDatabaseTask" breaks mongodb/laravel-mongodb "mongodb" drive registration Default "SwitchTenantDatabaseTask" breaks mongodb/laravel-mongodb "mongodb" driver registration Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant