How to Set .Env Values in Laravel Programmatically on the Fly

How to set .env values in laravel programmatically on the fly

Since Laravel uses config files to access and store .env data, you can set this data on the fly with config() method:

config(['database.connections.mysql.host' => '127.0.0.1']);

To get this data use config():

config('database.connections.mysql.host')

To set configuration values at runtime, pass an array to the config helper

https://laravel.com/docs/5.3/configuration#accessing-configuration-values

How to change variables in the .env file dynamically in Laravel?

There is no built in way to do that. If you really want to change the contents of the .env file, you'll have to use some kind of string replace in combination with PHP's file writing methods. For some inspiration, you should take a look at the key:generate command: KeyGenerateCommand.php:

$path = base_path('.env');

if (file_exists($path)) {
file_put_contents($path, str_replace(
'APP_KEY='.$this->laravel['config']['app.key'], 'APP_KEY='.$key, file_get_contents($path)
));
}

After the file path is built and the existence is checked, the command simply replaces APP_KEY=[current app key] with APP_KEY=[new app key]. You should be able to do the same string replacement with other variables.

Last but not least I just wanted to say that it might isn't the best idea to let users change the .env file. For most custom settings I would recommend storing them in the database, however this is obviously a problem if the setting itself is necessary to connect to the database.

How to change php dotenv (.env) variables dynamically in laravel or php?

putenv() work like a charm :

echo env('APP_ENV');
putenv('APP_ENV=testing');
echo env('APP_ENV');

Output:

staging
testing

.env file is unattached ...

Laravel: How to get or search or filter key values in env. file started with s.th

make a file inside the config folder example b_service.php

<?php

return [
'keys' => [
'survey' => env('B_SERVICE_TOKENS_SURVEY', ''),
'auth' => env('B_SERVICE_TOKENS_AUTH', ''),
]
];
  $bService = Config::get('b_service.keys');
// $bService contains all keys;

or

  $bService = config('b_service.keys');
Ref: https://laravel.com/docs/8.x/helpers#method-config

Update .env value via Laravel

try this

$path = base_path('.env');

if (file_exists($path)) {
file_put_contents($path, str_replace(
'APP_KEY='.$this->laravel['config']['app.key'], 'APP_KEY='.$key, file_get_contents($path)
));
}

taken from here stack answer

What's the correct way to set ENV variables in Laravel 5?

You can do it exactly the same as in Laravel 4:

$env = $app->detectEnvironment(array(
'local' => array('homestead')
));

*.env file are just used to put sensitive data that shouldn't be put into VCS. The same is in Laravel 4

but is seems that in last days default detectEnvironment was changed to:

$env = $app->detectEnvironment(function()
{
return getenv('APP_ENV') ?: 'production';
});

so you can use either setting variable from PC name or from ENV file.

If you use ENV based environment detection in main env file (by default .env file you need to add:

APP_ENV=local

Of course local here is local environment, you can change it into production or dev

At the moment the most important issue I see is that you need to remember when going on production to change this .env file content from APP_ENV=local to APP_ENV=production so in my opinion much better method is the old default method based on PC names.

Now ENV files. If you use ENV based environment detection, you should put into your ENV file only:

APP_ENV=local

Now you can create separate ENV files for your different environments for example:

.local.env :

 MY_DB=testdb

.production.env :

MY_DB=productiondb

and now in bootstrap.environment.php file you can modfiy:

if (file_exists(__DIR__.'/../.env'))
{
Dotenv::load(__DIR__.'/../');
}

into:

if (file_exists(__DIR__.'/../.env'))
{
Dotenv::load(__DIR__.'/../');

if (getenv('APP_ENV') && file_exists(__DIR__.'/../.' .getenv('APP_ENV') .'.env')) {
Dotenv::load(__DIR__ . '/../', '.' . getenv('APP_ENV') . '.env');
}
}

to load extra env file based on APP_ENV from main env file.

Now you will be able to use it in your other configuration file as always: $_ENV['MY_DB']



Related Topics



Leave a reply



Submit