I have to use file_put_content() function. But when the .env file $key value is empty then it will add new value other wise it show error. I need to merge old value.
private function setEnv($key, $value)
{
$path = base_path('.env');
if (file_exists($path)) {
file_put_contents($path, str_replace(
$key . '=' . env($key),
$key . '=' . $value,
file_get_contents($path)
));
}
}
Do you have any idea?