1public static function envUpdate($key, $value)
2 {
3 $path = base_path('.env');
4
5 if (file_exists($path)) {
6
7 file_put_contents($path, str_replace(
8 $key . '=' . env($key), $key . '=' . $value, file_get_contents($path)
9 ));
10 }
11 }
12
13And reload the page programatically again using js only once
14<script>
15 window.location.reload();
16</script>
1protected function updateDotEnv($key, $newValue, $delim='')
2{
3
4 $path = base_path('.env');
5 // get old value from current env
6 $oldValue = env($key);
7
8 // was there any change?
9 if ($oldValue === $newValue) {
10 return;
11 }
12
13 // rewrite file content with changed data
14 if (file_exists($path)) {
15 // replace current value with new value
16 file_put_contents(
17 $path, str_replace(
18 $key.'='.$delim.$oldValue.$delim,
19 $key.'='.$delim.$newValue.$delim,
20 file_get_contents($path)
21 )
22 );
23 }
24}