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?
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
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'
.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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With