{"id":"3da5627d6fcdc23f","repo":"laravel/framework","slug":"database-hosts-array-is-empty","errorCode":null,"errorMessage":"Database hosts array is empty.","messagePattern":"Database hosts array is empty\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Connectors/ConnectionFactory.php","lineNumber":245,"sourceCode":"                throw $exception;\n            }\n        };\n    }\n\n    /**\n     * Parse the hosts configuration item into an array.\n     *\n     * @param  array  $config\n     * @return array\n     *\n     * @throws \\InvalidArgumentException\n     */\n    protected function parseHosts(array $config)\n    {\n        $hosts = Arr::wrap($config['host']);\n\n        if (empty($hosts)) {\n            throw new InvalidArgumentException('Database hosts array is empty.');\n        }\n\n        return $hosts;\n    }\n\n    /**\n     * Create a new Closure that resolves to a PDO instance where there is no configured host.\n     *\n     * @param  array  $config\n     * @return \\Closure\n     */\n    protected function createPdoResolverWithoutHosts(array $config)\n    {\n        return fn () => $this->createConnector($config)->connect($config);\n    }\n\n    /**\n     * Create a connector instance based on the configuration.","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Connectors/ConnectionFactory.php#L227-L263","documentation":"Thrown by ConnectionFactory::parseHosts() when the 'host' config key resolves to an empty array (Arr::wrap on null or empty string yields an empty array). The factory builds a read/write resolver that needs at least one host to try.","triggerScenarios":"A connection config that has read/write sub-configs (or a host array) but no actual host value, e.g. ['driver'=>'mysql','host'=>null,'read'=>['host'=>null],'write'=>['host'=>[]]]. Triggered when ConnectionFactory::createReadPdoResolverWithHosts() calls parseHosts() on a read/write split setup.","commonSituations":"Missing DB_HOST env var in a read/write split; typo'd key (hostname vs host); empty string host coming from .env; deploying to an environment that didn't set the host var.","solutions":["Set the DB_HOST (and DB_READ_HOST / DB_WRITE_HOST if using a split) environment variable.","Verify config/database.php has a non-empty 'host' key under the connection and any read/write sub-arrays.","Run php artisan config:cache after fixing .env so the cached config is rebuilt.","Validate the resolved config at boot: assert !empty(config('database.connections.mysql.host'))."],"exampleFix":"// before (.env)\nDB_HOST=\n\n// after\nDB_HOST=127.0.0.1","handlingStrategy":"validation","validationCode":"$cfg = config('database.connections.'.$name);\nforeach (Arr::wrap($cfg['host'] ?? null) as $h) {\n    if (empty($h)) {\n        throw new \\RuntimeException(\"Connection [$name] has an empty host.\");\n    }\n}","typeGuard":"function hasNonEmptyHosts(array $config): bool {\n    return ! empty(Arr::wrap($config['host'] ?? null));\n}","tryCatchPattern":null,"preventionTips":["Always set DB_HOST (and DB_READ_HOST/DB_WRITE_HOST for splits) in .env.","Add a boot-time config assertion for every connection's host key.","Re-run config:cache after changing host env vars.","Validate read/write sub-arrays each have a host when using a split."],"tags":["configuration","connection","environment"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}