{"record":{"id":"2500ac282ca11f8c","repo":"laravel/framework","slug":"invalid-database-connection-db","errorCode":null,"errorMessage":"Invalid database connection [{$db}].","messagePattern":"Invalid database connection \\[(.+?)\\]\\.","errorType":"exception","errorClass":"UnexpectedValueException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Console/DbCommand.php","lineNumber":85,"sourceCode":"\n        return self::SUCCESS;\n    }\n\n    /**\n     * Get the database connection configuration.\n     *\n     * @return array\n     *\n     * @throws \\UnexpectedValueException\n     */\n    public function getConnection()\n    {\n        $connection = $this->laravel['config']['database.connections.'.\n            (($db = $this->argument('connection')) ?? $this->laravel['config']['database.default'])\n        ];\n\n        if (empty($connection)) {\n            throw new UnexpectedValueException(\"Invalid database connection [{$db}].\");\n        }\n\n        if (! empty($connection['url'])) {\n            $connection = (new ConfigurationUrlParser)->parseConfiguration($connection);\n        }\n\n        if ($this->option('read')) {\n            $connection = $this->mergeConnectionConfiguration($connection, 'read');\n        } elseif ($this->option('write')) {\n            $connection = $this->mergeConnectionConfiguration($connection, 'write');\n        } elseif (! $this->option('pooled') && ($connection['driver'] ?? null) === 'pgsql' && ($connection['pooled'] ?? false) === true && ! empty($connection['direct'])) {\n            $connection = $this->mergeConnectionConfiguration($connection, 'direct');\n        }\n\n        return $connection;\n    }\n\n    /**","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Console/DbCommand.php#L67-L103","documentation":"Thrown by DbCommand::getConnection() (the artisan `db` console command) when the resolved connection config is empty. It looks up database.connections.<name> using the command argument or the default connection; if that entry is absent or empty, the command cannot build a PDO config and aborts.","triggerScenarios":"Running `php artisan db foo` where 'foo' is not defined in database.connections; running `php artisan db` with database.default pointing at an undefined connection; the connection config resolves to null because .env vars are unset.","commonSituations":"Typo in the connection name argument; referencing an environment-specific connection that is not present in the current config; misconfigured database.default; stale config cache (php artisan config:cache) baked in before the connection was added.","solutions":["Add the connection to config/database.php under database.connections.<name> with a valid driver.","Verify the name passed to `artisan db` matches a configured connection key exactly.","Ensure database.default is set to an existing connection (DB_CONNECTION).","Clear the config cache if you recently edited config: php artisan config:clear."],"exampleFix":"// before\nphp artisan db readreplica   # not in config\n\n// after\n// config/database.php: add 'readreplica' => [...]\nphp artisan db readreplica","handlingStrategy":"validation","validationCode":"$name = $requestedName ?? config('database.default');\nif (empty(config('database.connections.'.$name))) {\n    throw new \\UnexpectedValueException(\"Connection '{$name}' is not configured.\");\n}","typeGuard":"function connectionIsConfigured(string $name): bool {\n    return ! empty(config('database.connections.'.$name));\n}","tryCatchPattern":null,"preventionTips":["Verify connection names against config('database.connections') before invoking `artisan db`.","Keep database.default pointing at a valid connection.","Run config:clear after adding connections."],"tags":["database","console","artisan","configuration","laravel"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}