3

Example, if my .env is

APP_ENV=local
APP_URL=http://localhost:8888/
APP_DEBUG=true
FACEBOOK_APP_ID = ABC

I know in Laravel we can do access our .env file by doing this

echo env('APP_ENV'); --> 'local'
echo env('APP_URL'); --> 'http://localhost:8888/'

but I wonder if there is a way to programmatically set it

Ex. env('APP_ENV') == 'production';
code-8
  • 54,650
  • 106
  • 352
  • 604
  • You'd have to write to the file, but it's typically best for security to have your Laravel install configured not to have permissions to do so. `config('app.env', 'production')` would override the config value for the *current* request, though. – ceejayoz Mar 10 '17 at 20:54
  • I just using APP_ENV as an example, but I actually want to update something else like FACEBOOK_APP_ID, base on a certain account, etc ... – code-8 Mar 10 '17 at 21:02
  • If I want to update my `FACEBOOK_APP_ID` in my .env file will this `config('FACEBOOK_APP_ID.env', '123')` works ? Or something similar to that ? – code-8 Mar 10 '17 at 21:03
  • No, that won't. You really should be changing the `.env` file either via a deployment system or by editing it directly. – ceejayoz Mar 10 '17 at 21:04
  • 1
    .env file isn't supposed to be your config file, but rather your template for your config. Your settings should be stored outside, ideally in your hosting as environment variables. Also it's a very bad practice security-wise to be able to edit it like this... You really don't want to do that. – walther Mar 10 '17 at 21:08

4 Answers4

10

This might help.

function setEnv($name, $value)
{
    $path = base_path('.env');
    if (file_exists($path)) {
        file_put_contents($path, str_replace(
            $name . '=' . env($name), $name . '=' . $value, file_get_contents($path)
        ));
    }
}
setEnv('APP_ENV','abc');
Saiful Islam
  • 620
  • 5
  • 13
  • Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. Thanks for improving the answer's reference value and making it more understandable! – Tim Diekmann Jun 21 '18 at 10:24
2

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

Community
  • 1
  • 1
ATechGuy
  • 1,240
  • 8
  • 13
  • What if the I want to update a custom key like FACEBOOK_APP_ID to anything string. Will your suggestion still work ? – code-8 Mar 10 '17 at 21:07
  • this should work for any var you want to update just swap out "APP_KEY" for "FACEBOOK_APP_ID" – ATechGuy Mar 10 '17 at 21:09
  • Ok. What is this `$this->laravel['config']['app.key']` do ? I don't think I should that when I want to use this `FACEBOOK_APP_ID`... – code-8 Mar 10 '17 at 21:14
  • that takes the current text for "APP_KEY" and the next line replaces that key with the new one – ATechGuy Mar 10 '17 at 21:15
  • Can you check my first param of str_replace() ? `'FACEBOOK_APP_ID='.$this->laravel['config']['app.key']` ... – code-8 Mar 10 '17 at 21:19
  • Just curious, if I should it blank or leave it as what you suggested. – code-8 Mar 10 '17 at 21:19
  • please try and do a little yourself to see what you come up with, this will help you learn the best. – ATechGuy Mar 10 '17 at 21:20
  • This `$this->laravel['config']['app.key']` doesn't work on my senario. because my FACEBOOK_APP_ID is not == to app.key .. – code-8 Mar 10 '17 at 21:22
1

You can use:

putenv("APP_ENV=production");

because env is just a convenience wrapper around getenv which does some type conversion.

Note: this is not permanent as it will be overwritten by .env when the next request happens.

apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • @Linesofcode that's true, this will not work in Laravel 5.8+ because the method `env` was changed and no longer uses `getenv` – apokryfos Aug 26 '20 at 12:03
0

You can easily update or insert the variable in .env file by using syamsoul/laravel-set-env package.

.

  1. Install via composer
composer require syamsoul/laravel-set-env

.

  1. Just run the command
php artisan souldoit:set-env
  • You will be prompted about the variable name and the value.
  • Or, just straightaway put the variable name and value into the argument.

.

For example:

php artisan souldoit:set-env "APP_NAME=My New Laravel Name"

P/S: Tested on Laravel 10 and above and it is working fine.

Syamsoul Azrien
  • 2,534
  • 4
  • 32
  • 55