diff --git a/src/Tenant.php b/src/Tenant.php index db9997d0..418b78fa 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -30,7 +30,7 @@ class Tenant extends Model public function getConnectionName() { - return config('tenancy.storage.db.connection', 'central'); + return config('tenancy.storage.db.connection') ?: config('database.default'); } public static function getAllTenants(array $uuids) diff --git a/tests/TenantStorageTest.php b/tests/TenantStorageTest.php index 806e19b0..471dedaf 100644 --- a/tests/TenantStorageTest.php +++ b/tests/TenantStorageTest.php @@ -4,6 +4,7 @@ namespace Stancl\Tenancy\Tests; use Stancl\Tenancy\StorageDrivers\RedisStorageDriver; use Stancl\Tenancy\StorageDrivers\DatabaseStorageDriver; +use Stancl\Tenancy\Tenant; class TenantStorageTest extends TestCase { @@ -140,4 +141,15 @@ class TenantStorageTest extends TestCase tenancy()->put('string', 'foo'); $this->assertSame('string', \gettype(tenancy()->get('string'))); } + + /** @test */ + public function tenant_model_uses_correct_connection() + { + config(['tenancy.storage.db.connection' => 'foo']); + $this->assertSame('foo', (new Tenant)->getConnectionName()); + + config(['tenancy.storage.db.connection' => null]); + config(['database.default' => 'foobar']); + $this->assertSame('foobar', (new Tenant)->getConnectionName()); + } }