In LARAVEL .ENV file read SYSTEM VARIABLE - laravel

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

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
Step 2 : Use them into config/database.php
Step 3 : Use them into config/mail.php
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}

Related

How to rename .env variables in package.json?

What I have
I have multiple projects using Percy for Cypress where I set the PERCY_TOKEN env variable inside the .env file. The token is different for each project. In the CI I set different env variables for each project, but locally I have to do it in the .env file. Because of this, I have to edit the .env file whenever I change between projects.
Goal
I would like to set them in the .env file this way:
PROJECT_A_PERCY_TOKEN=tokenhash1
PROJECT_B_PERCY_TOKEN=tokenhash2
So later I could rename these variables to PERCY_TOKEN, eliminating the need to constantly change the .env file.
What I tried
I'm trying to do this inside the package.json file's scripts property. Unfortunately echo $PROJECT_A_PERCY_TOKEN prints nothing. I know that I could create a shell/python/js script that parses the .env file, then passes the value back or calls npm run directly but I would like to do this without an external script.
Problem
It appears to me that I can't access the env variables inside package.json. Is there a way to rename the variable only using the npm script?
tl;dr
If the package you try to configure has the ability to do configuration via a JavaScript file, you can add the renaming at the beginning of it:
process.env.PERCY_TOKEN = process.env.CYPRESS_PERCY_SALESFORCE_TOKEN;
Explanation
While this isn't the solution I was looking for, it is a workaround for this specific use case. Percy supports JavaScript config files so I migrated my YAML config file, then I logged process.env and the .env file's variables were there, so I just need to copy the correct one. This might work for other packages that support JavaScript config files (or some alternative kind of hook/preloader where custom code can be placed), but if they don't, then the question is still unanswered.

How can I save changes in .env file in Laravel?

I have a huge problem with .env file in Laravel. When I make a change in .env file, it is not changed. I think all of you know what I'm talking about. I run this php artisan config:cache, but yet, nothing changed! I echo out a variable in .env file and nothing changed. What can I do for that?
When you cache the configuration all calls to env will return null because it does not load the .env any more. You should not have any calls to env outside of the config files.
You use the configuration system to get the values you want, which is why the config files make calls to env to get values from the environment.
If you using php artisan serve you have to reload it after changing .env file

add new env file to laravel for new server environment

i'm taking over a project from a client to add functionalities. I quite new in the development of Laravel 5.5
There are already 3 .env files in my project:
.env
.env.example
.env.live
.env.live is for production (APP/ENV=production) and runs on aws, the other 2 are for local use.
Now I have a new AWS EC2 server, database and so on and need a new env file to use these new instances. Let's say I create a new .env file called .env.dev
How how can I switch between the .env.dev and .env.live? Or how do I use this .env.dev?
If you're using Elastic Beanstalk, go to your environment, then click the Configuration menu item. From there click "Modify" in the "Software" section. A page will appear where you can add Environment Properties as key->value pairs.
You can then delete the .env files. These properties will be read by your Laravel app just like it reads the .env files.
You can also use the symbolic links
Just Delete .env from both of the server and create a symbolic link like.
ln -s .env.live .env // for live
ln -s .env.dev .env // for dev server
You can also keep this command inside your deployment script if you are using any CI/CD.
On every server you only need .env file to execute. If you have three files
.env
.env.live
.env.example
.env.other
you can create multiple files with .env.*. But when you will execute your application then your .env file will execute.
Now if you want to add new additional settings to your environment then you just need to add in .env file. and also specify all other .env.* files and change values according to server.
When you will deploy your application on live server. Then you just need to rename your .env.live file to .env and set configurations to that file.
cp .env.live .env
Note: if you already have .env file on your server(as you said your application is already on production server.) then you just need to add your environment settings to that file.

Laravel - .env vs database.php

I am confused by Laravels .env file VS other settings
I have a .env file which has my sql database settings inside, but I also have a file in config/database.php that has these settings inside.
When I deploy my application to an Elastic Beanstalk instance, which of these files is it using for the database settings?
.env is short for environment and thus that is your environment configurations.
The database.php configuration contains non-critical information.
You obviously won't have your database's username's password in your source control or available in the code.
In order to keep everything safe or to keep information saved that is environment-defined... you keep them in .env file
Laravel will prioritize .env variables.
A little more detailed answer:
The config Files are where you store all configurations. You shouldn't add any username, passwords or other secret informations in them, because they will be in your source control.
All secret informations and all environment dependant informations should be stored in your .env file. With this you can have different configuration values in local/testing/production with just a different .env file.
In your config files you access the information in you .env files, if necessary.
When use what from another answer
use env() only in config files
use App::environment() for checking the environment (APP_ENV in .env).
use config('app.var') for all other env variables, ex. config('app.debug')
create own config files for your own ENV variables. Example:
In your .env:
MY_VALUE=foo
example config app/myconfig.php
return [
'myvalue' => env('MY_VALUE', 'bar'), // 'bar' is default if MY_VALUE is missing in .env
];
Access in your code:
config('myconfig.myvalue') // will result in 'foo'
In your ".env" file you have your settings. in the ".php" files like your "database.php" file this is the default value for the property and normally, the corresponding value in the ".env" file is use here with this syntax : 'database' => env('database', 'default_value'),
The .env file is the first file it will use for configs. In case values are missing inside the .env file Laravel will check the config files. I primairly use those as backups.

It's possible re-read .env file on Laravel 5.X

I needs to work with .env file on laravel.
But on some circumstances I need put a new file (.env) with new values, and I need update all environment vars and best way it's read .env file.
I don't see how to re-read .env file on Laravel (read for re-assign values to all env vars)

Resources