laravel update config file dynamically code example
Example 1: laravel set config value dynamically
Config::set('custom.my_val', 'mysinglelue');
Example 2: laravel update env file dynamically
protected function updateDotEnv($key, $newValue, $delim='')
{
$path = base_path('.env');
$oldValue = env($key);
if ($oldValue === $newValue) {
return;
}
if (file_exists($path)) {
file_put_contents(
$path, str_replace(
$key.'='.$delim.$oldValue.$delim,
$key.'='.$delim.$newValue.$delim,
file_get_contents($path)
)
);
}
}