3

I have AWS server where we are setting all system environment variables. For reference system environment variables like DATABASE connection, EMAIL CLIENT configurations and some other configuration.

We can read those system variables into PHP file as well as into terminal. But my requirement is to read system variable into .env file of laravel

Following works into PHP but not into .env file

env('VARIABLE_NAME')             
getenv('VARIABLE_NAME')
$_ENV['VARIABLE_NAME']

The reason for this is that we are setting system wide environment variable to avoid the secrets being stored in Physical files. And as a solution changing directly into module files each time is not practical solution. Is there any way by which we can Read/Set SYSTEM VARIABLE INTO .env file ??

Anyone any help would highly be appreciated. Thanks

enter image description here

4
  • 1
    you don't need them into .env if they are environmental vars they should already be available to you without .env, .env is for when you don't have actual environmental vars set Commented Jan 18, 2021 at 7:29
  • It's not recommended to do that since based on OS and distors they act differently and you can't rely on it. Commented Jan 18, 2021 at 7:40
  • Thank you for your comments. Actually system variable names are based on project names. That means "DB_DATABASE" -> Variable name is different among different projects. So there is no way for me to set same database variable for different projects. Similar for other variablels. Commented Jan 18, 2021 at 8:33
  • you can reference other variables in the .env file .... if you are on a recent version of Laravel you will see an example of that in the .env file already Commented Jan 18, 2021 at 11:03

1 Answer 1

2

There is no working way I found to use or read variable within .env file of laravel. However you can directly read system environment variables into config files of your laravel framework as given example below.

Set following and all other variables into your .htacess file and retrieve them back

hostname
DBname
DBusername
DBpassword
smtp_host
smtp_port
smtp_user
smtp_password
ssl_ca

Step 1 : Set those variables into .htaccess file

enter image description here

Step 2 : Use them into config/database.php

enter image description here

Step 3 : Use them into config/mail.php

enter image description here

I used this method for my laravel project as I don't find any solution for reading variables into .env file. Some says that following method works but it did not worked for me.

DB_DATABASE ${VARIABLE_NAME}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for this solution It's working. But do you know how to read variable into .env file ??
I couldn't find 100% working solution to read variables into .env file. That is why i used this solution.
Just LARA_ENV_VAR=${SYSTEM_ENV_VAR} in laravel .env file worked for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.