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

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)

Related

Load data from exported environment variables in laravel

The context is I currently working with laravel framework. Is it possible if I want to move all configs in .env file into system environment variables? so I don't need .env file anymore. So I will do:
export APP_NAME=Laravel
export APP_ENV=local
export APP_KEY=
export APP_DEBUG=true
export APP_URL=http://localhost
...
then remove the .env file. What should I do to make laravel read all configurations from exported environment variables instead of read from .env file? Thank you
Can't understand why you want overhead of environment variables when Laravel itself give the functionality of accessing .env variables using
env('APP_NAME')
All of the variables listed in the .env file will be loaded into the $_ENV PHP super-global when your application receives a request so alternatively you can use $_ENV['APP_NAME'] also.

In LARAVEL .ENV file read SYSTEM VARIABLE

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}

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.

Resources