Answers for "update laravel .env variables dynamically"

PHP
0

update laravel .env variables dynamically

/**
     * Update Laravel Env file Key's Value
     * @param string $key
     * @param string $value
     */
    public static function envUpdate($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)
            ));
        }
    }
Posted by: Guest on May-12-2021
0

laravel update env file dynamically

protected function updateDotEnv($key, $newValue, $delim='')
{

    $path = base_path('.env');
    // get old value from current env
    $oldValue = env($key);

    // was there any change?
    if ($oldValue === $newValue) {
        return;
    }

    // rewrite file content with changed data
    if (file_exists($path)) {
        // replace current value with new value 
        file_put_contents(
            $path, str_replace(
                $key.'='.$delim.$oldValue.$delim, 
                $key.'='.$delim.$newValue.$delim, 
                file_get_contents($path)
            )
        );
    }
}
Posted by: Guest on November-27-2020

Code answers related to "update laravel .env variables dynamically"

Browse Popular Code Answers by Language