fightstarr20
fightstarr20

Reputation: 12568

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?

Upvotes: 10

Views: 7284

Answers (4)

ndberg
ndberg

Reputation: 3971

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'

Upvotes: 3

Shafiq al-Shaar
Shafiq al-Shaar

Reputation: 1323

.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.

Upvotes: 8

killstreet
killstreet

Reputation: 1332

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.

Upvotes: 2

Pierre-Adrien Maison
Pierre-Adrien Maison

Reputation: 610

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'),

Upvotes: 2

Related Questions