Marlon Dyck
Marlon Dyck

Reputation: 615

How can I use multiple .env files for the same environment in Laravel?

I work on an application where our database administrator has mandated that the production database credentials be inaccessible to anyone that's not a database admin or the web app itself. He suggested we create a second .env file that contains only the database credentials so that he can lock down that file. Is there a way to do this? Essentially we would be reading config values from both .env files.

For example our app config file might look like

return [
    'some_configuration' => env1('SOME_CONFIGURATION'),
]

While the database config file might look like

return [
    'database_password' => env2('DB_PASSWORD')
]

How can I read configs from multiple .env files like this?

Upvotes: 0

Views: 1787

Answers (1)

ceejayoz
ceejayoz

Reputation: 180023

Your best bet will probably be using an arbitrary file and getting its contents via file_get_contents:

return [
    'database_password' => trim(file_get_contents('.secret_file'))
]

(The trim is in case your database administrator leaves a stray \n or space or something.)

Upvotes: 1

Related Questions