diff --git a/src/Illuminate/Database/DatabaseManager.php b/src/Illuminate/Database/DatabaseManager.php index 6f8867c4e51..4de9ecc16ce 100755 --- a/src/Illuminate/Database/DatabaseManager.php +++ b/src/Illuminate/Database/DatabaseManager.php @@ -91,7 +91,7 @@ public function __construct($app, ConnectionFactory $factory) */ public function connection($name = null) { - $name = $name ?: $this->getDefaultConnection(); + $name = $this->mapConnectionName($name); [$database, $type] = $this->parseConnectionName($name); @@ -166,6 +166,19 @@ public function connectUsing(string $name, array $config, bool $force = false) return tap($connection, fn ($connection) => $this->connections[$name] = $connection); } + /** + * Map the given connection name to its configuration name or default if not provided. + * + * @param string $name + * @return string + */ + protected function mapConnectionName($name) + { + $name = $name ?: $this->getDefaultConnection(); + + return $this->app['config']['database.'.$name] ??= $name; + } + /** * Parse the connection into an array of the name and read / write type. * @@ -174,7 +187,7 @@ public function connectUsing(string $name, array $config, bool $force = false) */ protected function parseConnectionName($name) { - $name = $name ?: $this->getDefaultConnection(); + $name = $this->mapConnectionName($name); return Str::endsWith($name, ['::read', '::write']) ? explode('::', $name, 2) : [$name, null]; @@ -217,7 +230,7 @@ protected function makeConnection($name) */ protected function configuration($name) { - $name = $name ?: $this->getDefaultConnection(); + $name = $this->mapConnectionName($name); $connections = $this->app['config']['database.connections']; @@ -304,7 +317,7 @@ protected function setPdoForType(Connection $connection, $type = null) */ public function purge($name = null) { - $name = $name ?: $this->getDefaultConnection(); + $name = $this->mapConnectionName($name); $this->disconnect($name);