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


The .env file has the following details −

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:BUdhVZjOEzjFihHKgt1Cc+gkQKPoA4iH98p5JwcuNho=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DRIVER=public
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
SERVER_ADDR = 127.0.0.1

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

This file is generated when you do your Laravel installation. It has all the common environment variables like APP_NAME, APP_URL, your DB connection, etc.

The above environment variables are used inside the config/ folder which holds all the configuration files. The folder structure for config/ is as follows −

The values inside the .env file will differ when you run your application locally, in your staging environment, and production. Since the .env source contents differ based on the environment worked it is best not to commit the file to your repository.

To get the environment you are working in your application you can make use of the App Facade class: use Illuminate\Support\Facades\App;

Example 1

to fetch the environment type using App::environment()

use Illuminate\Support\Facades\App; $environment = App::environment(); echo $environment;

Output

The output will be −

local

The environment variable APP_DEBUG is read inside config/app.php. When set to true it will display the information about error messages on the screen. The value is fetched from the .env file inside config/app.php. It should be set to true when you work locally and false when it is in production.

Now let us understand how to change the environment variables dynamically. There is no straightforward way to change it and one of the ways you can do is as follows:

Example 2

To change environment variables dynamically

$path = base_path('.env'); $test = file_get_contents($path); if (file_exists($path)) { file_put_contents($path, str_replace('APP_ENV=local', 'APP_ENV=production', $test)); }

Output

In the above example, we are getting the file path for the .env file. Later the variable APP_ENV=local is replaced with APP_ENV=production. Now when you check the .env file you will get the following –

APP_NAME=Laravel
APP_ENV=production
APP_KEY=base64:BUdhVZjOEzjFihHKgt1Cc+gkQKPoA4iH98p5JwcuNho=
APP_DEBUG=true
APP_URL=http://localhost

Example 3

to fetch environment variables using env().

The env() method helps you to fetch the values set to the environment variables in the .env file. You can pass the variable name to the env() method and it will return you the value set in the .env file.

echo env('APP_URL'); echo "<br/>"; echo env('APP_ENV'); echo "<br/>"; echo env('APP_KEY');

Output

The output of the above code is −

http://localhost 
local 
base64:BUdhVZjOEzjFihHKgt1Cc+gkQKPoA4iH98p5JwcuNho=

Example 4

You can also make use of $_ENV global variable to get all the environment variables and their values.

print_r($_ENV);

Output

The output of the above code is −

Array(
   [APP_NAME] => Laravel
   [APP_KEY] => base64:BUdhVZjOEzjFihHKgt1Cc+gkQKPoA4iH98p5JwcuNho=
   [APP_DEBUG] => true
   [APP_URL] => http://localhost
   [LOG_CHANNEL] => stack
   [LOG_DEPRECATIONS_CHANNEL] => null
   [LOG_LEVEL] => debug
   [DB_CONNECTION] => mysql
   [DB_HOST] => 127.0.0.1
   [DB_PORT] => 3306
   [DB_DATABASE] => test
   [DB_USERNAME] => root
   [DB_PASSWORD] =>
   [BROADCAST_DRIVER] => log
   [CACHE_DRIVER] => file
   [FILESYSTEM_DRIVER] => public
   [QUEUE_CONNECTION] => sync
   [SESSION_DRIVER] => file
   [SESSION_LIFETIME] => 120
   [MEMCACHED_HOST] => 127.0.0.1
   [REDIS_HOST] => 127.0.0.1
   [REDIS_PASSWORD] => null
   [REDIS_PORT] => 6379
   [SERVER_ADDR] => 127.0.0.1
   [MAIL_MAILER] => smtp
   [MAIL_HOST] => mailhog
   [MAIL_PORT] => 1025
   [MAIL_USERNAME] => null
   [MAIL_PASSWORD] => null
   [MAIL_ENCRYPTION] => null
   [MAIL_FROM_ADDRESS] => null
   [MAIL_FROM_NAME] => Laravel
   [AWS_ACCESS_KEY_ID] =>
   [AWS_SECRET_ACCESS_KEY] =>
   [AWS_DEFAULT_REGION] => us-east-1
   [AWS_BUCKET] =>
   [AWS_USE_PATH_STYLE_ENDPOINT] => false
   [PUSHER_APP_ID] =>
   [PUSHER_APP_KEY] =>
   [PUSHER_APP_SECRET] =>
   [PUSHER_APP_CLUSTER] => mt1
   [MIX_PUSHER_APP_KEY] =>
   [MIX_PUSHER_APP_CLUSTER] => mt1
)

Updated on: 30-Aug-2022

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements