diff --git a/src/Illuminate/Database/Connectors/PostgresConnector.php b/src/Illuminate/Database/Connectors/PostgresConnector.php index 5129b4b4474d..bd5b7c228b55 100755 --- a/src/Illuminate/Database/Connectors/PostgresConnector.php +++ b/src/Illuminate/Database/Connectors/PostgresConnector.php @@ -40,7 +40,7 @@ public function connect(array $config) // database. Setting this DB timezone is an optional configuration item. $this->configureTimezone($connection, $config); - $this->configureSchema($connection, $config); + $this->configureSearchPath($connection, $config); // Postgres allows an application_name to be set by the user and this name is // used to when monitoring the application with pg_stat_activity. So we'll @@ -81,34 +81,42 @@ protected function configureTimezone($connection, array $config) } /** - * Set the schema on the connection. + * Set the search_path on the connection. * * @param \PDO $connection * @param array $config * @return void */ - protected function configureSchema($connection, $config) + protected function configureSearchPath($connection, $config) { - if (isset($config['schema'])) { - $schema = $this->formatSchema($config['schema']); + if (isset($config['searchpath']) || isset($config['schema'])) { + $searchPath = $this->formatSearchPath($config['searchpath'] ?? $config['schema']); - $connection->prepare("set search_path to {$schema}")->execute(); + $connection->prepare("set search_path to {$searchPath}")->execute(); } } /** - * Format the schema for the DSN. + * Format the search path for the DSN. * - * @param array|string $schema + * @param array|string $searchPath * @return string */ - protected function formatSchema($schema) + protected function formatSearchPath($searchPath) { - if (is_array($schema)) { - return '"'.implode('", "', $schema).'"'; + if (is_array($searchPath)) { + $schemas = array_map(function ($item) { + return '"'.$item.'"'; + }, $searchPath); + + $preparedSchemas = array_map(function ($item) { + return ":$item"; + }, $schemas); + + return implode(', ', array_combine($preparedSchemas, $schemas)); } - return '"'.$schema.'"'; + return implode(', ', [":$searchPath" => '"'.$searchPath.'"']); } /**