1

Is there any ways that I could change my .env file values from controllers in laravel?

I've found this answer but it returns

Undefined property: App\Http\Controllers\Admin\PerformanceController::$laravel

code

$path = base_path('.env');
$key = false;

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

I want to have options in my admin panel to change debug mode between true or false, same as we have artisan commands in controller like Artisan::call('down') or Artisan::call('up') something like that.

Update

Now I have this code

$path = base_path('.env');
$key = 'true';

if (file_exists($path)) {
  file_put_contents($path, str_replace(
    'APP_DEBUG='.config('app.debug'), 'APP_DEBUG='.$key, file_get_contents($path)
  ));
}

this code does work but the issue is that it doesn't remove old value.

Before

APP_DEBUG=false

After

APP_DEBUG=truefalse
or
APP_DEBUG=falsefalse

any idea?

mafortis
  • 6,750
  • 23
  • 130
  • 288
  • I think this link might be helpful: https://laravel-tricks.com/tricks/change-the-env-dynamically – Hiren Gohel Jan 24 '19 at 06:42
  • https://stackoverflow.com/q/40450162/3854365 check this answer – Iftikhar uddin Jan 24 '19 at 06:44
  • @HirenGohel I didn't understand that. where data comes from? where we set values? how to update? it's so incomplete. – mafortis Jan 24 '19 at 06:45
  • @mafortis: See there, `something()` function contains `$env_update` array which you want to update data into env file. And calling other function named as `changeEnv` to update the datas. – Hiren Gohel Jan 24 '19 at 06:52
  • @mafortis:The author has also created a package for the same, you can review it here: https://github.com/Brotzka/laravel-dotenv-editor – Hiren Gohel Jan 24 '19 at 06:59
  • From the link that you provided, use the [next answer](https://stackoverflow.com/a/50965881/3226121) – ljubadr Jan 24 '19 at 07:00
  • @HirenGohel that package has 2 issues i used to use that, `1` security `2` generated file can't accept white space. – mafortis Jan 24 '19 at 07:11

3 Answers3

1

it is not good idea to change your .env configuration. instead of that, use this code where you want change your APP_KEY.

be sure you did not cache your config

config(['app.key' => 'YOUR_NEW_KEY']);
  • in that case i should leave my env values empty? – mafortis Jan 24 '19 at 06:37
  • no, it will replace default configuration of config/app.php whenever you use this part of code. when you do not use this code, app.php use .env values to populate its values. – Mehrdad Mahdian Jan 24 '19 at 06:46
  • that's why i need to replace env file values because laravel uses them as priority, and this code didn't change anything in my config/app.php as well. – mafortis Jan 24 '19 at 06:48
  • I got what you want, you could do something which i did not tested that before. create new Middleware for your application and place it on the top of $middleware property of app/http/kernel.php class. on the handle(), set your new configuration. please tell me it was helpful or not after test.it is very close to entry point of your application – Mehrdad Mahdian Jan 24 '19 at 07:14
0

instead of using $this->laravel['config']['app.key'] try config('app.key')

  • ok this doesn't return the error but there is strange issue, when i use `config('app.key')` it changes the `APP_KEY` to `APP_DEBUG` in env file but when I use `config('app.debug')` it doesn't do anything. – mafortis Jan 24 '19 at 07:05
  • Because you told your code to find the `APP_KEY` and to replace with `APP_DEBUG`. Change `APP_KEY` to `APP_DEBUG` – ljubadr Jan 24 '19 at 07:07
  • now i have this `'APP_DEBUG='.config('app.debug'), 'APP_DEBUG='.$key, file_get_contents($path)` but doesn't make change in env file – mafortis Jan 24 '19 at 07:10
  • Can you explain that further? Why do you think that this solves the problem? – Nico Haase Jan 24 '19 at 09:16
0

use this DotenvEditor::setKey('APP_KEY', 'new_value')->save();