通过Laravel更新.env值(Update .env value via Laravel)

例如,如果我的.env

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

我知道在Laravel中我们可以通过这样做来访问我们的.env文件

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

但我想知道是否有办法以编程方式设置它

Ex. env('APP_ENV') == 'production';

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';

最满意答案

尝试这个

$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) )); }

取自这里的堆栈答案

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

更多推荐