$values * * @throws Exception */ public function writeToEnvironment(array $values = []): void { $path = base_path('.env'); if (!file_exists($path)) { throw new Exception('Cannot locate .env file, was this software installed correctly?'); } $saveContents = file_get_contents($path); if ($saveContents === false) { $saveContents = ''; } collect($values)->each(function ($value, $key) use (&$saveContents) { $key = strtoupper($key); $saveValue = sprintf('%s=%s', $key, $this->escapeEnvironmentValue($value ?? '')); if (preg_match_all('/^' . $key . '=(.*)$/m', $saveContents) < 1) { $saveContents = $saveContents . PHP_EOL . $saveValue; } else { $saveContents = preg_replace('/^' . $key . '=(.*)$/m', $saveValue, $saveContents); } }); file_put_contents($path, $saveContents); } }