title: Changing env values via controller in laravel
body: <p>Is there any ways that I could change my <code>.env</code> file values from controllers in laravel?</p>
  
  <p>I've found <a href="https://stackoverflow.com/questions/42727510/update-env-value-via-laravel/42727702#42727702">this answer</a> but it returns</p>
  
  <blockquote>
    <p>Undefined property:
    App\Http\Controllers\Admin\PerformanceController::$laravel</p>
  </blockquote>
  
  <h1>code</h1>
  
  <pre><code>$path = base_path('.env');
  $key = false;
  
  if (file_exists($path)) {
    file_put_contents($path, str_replace(
      'APP_KEY='.$this-&gt;laravel['config']['app.key'], 'APP_DEBUG='.$key, file_get_contents($path)
     ));
  }
  </code></pre>
  
  <p>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 <code>Artisan::call('down')</code> or <code>Artisan::call('up')</code> something like that.</p>
  
  <h1>Update</h1>
  
  <p>Now I have this code</p>
  
  <pre><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)
    ));
  }
  </code></pre>
  
  <p>this code does work but the issue is that it doesn't remove old value.</p>
  
  <h3>Before</h3>
  
  <pre><code>APP_DEBUG=false
  </code></pre>
  
  <h3>After</h3>
  
  <pre><code>APP_DEBUG=truefalse
  or
  APP_DEBUG=falsefalse
  </code></pre>
  
  <p>any idea?</p>