Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davidmarsmalow's avatar

Laravel multiple .env file value for different environtments

I have multiple branch for each environments (let's say: development, testing, production) and for each branch i deploy to specific server with different url and also connection (db host, etc). Is there a way so i can push my .env file (eventhough it is a bad practice because i want to keep track of every changes my team did) but not replacing and conflicting each server if i do a merge request?

I want to use the same inheritance behaviour from .ini file like:

[production]
APP_NAME="App Production"
KEY="Value for all env"

[testing: production]
APP_NAME="App Testing"

[development : production]
APP_NAME="App Development"

But it doesn't work as i expected in .env file in Laravel, i have tried this because GPT has told me so:

APP_ENV=dev
APP_NAME=
APP_NAME_DEV="App Development"
APP_NAME_TESTING="App Testing"
APP_NAME_PRODUCTION="App Production"

Is there a way to do it without any needs to change other files in Laravel. Can someone help me with any suggestion on how i should i it? Anything would be appreciated, thanks a lot.

0 likes
9 replies
Sinnbeck's avatar

You can have multiple env files. Laravel will check for one that matches the environment. For instance if you create .env.testing, and set the ENVIRONMENT in phpunit.xml to "testing", it will use that file if it exists.

But you cannot have 1 env file that works for different environments.

Also for development it is suggested to change the .env.example to work with the dev environment. Then new devs can just do cp .env.example .env to get a working env file

davidmarsmalow's avatar

@Sinnbeck i have created a new .env file named .env.testing, and add this line in phpunit.xml inside the tag:

<env name="ENVIRONMENT" value="testing"/>

but after i get the env, i am still getting the value from .env and not .env.testing.

Do i have to run something first or it just loaded automatically?

davidmarsmalow's avatar

@Sinnbeck oh my apologies, my case is not in the test but on laravel app in general, just a regular dd(env(APP_NAME)) in routes. My expectation is i can switch between each working environment (such as development, or testing, or production) in git branch with still pushing my .env to git so i still can keep track of every .env changes.

Thankfully I have find a way to do that with creating a multiple .env file like this:

\.env
\.env.development
\.env.testing
\.env.production

This way I can keep track changes and specify which .env file should every server use while all branch is having the same version of all .env file (1:1). So I just push every .env file to git to keep track of them, and then set the environment in my web server by adding this in my virtual hosts (nginx):

fastcgi_param APP_ENV development

and so for every server without replacing my .env file on branch merging

Thankyou for your help @sinnbeck . And I hope other people can use this as a references.

Snapey's avatar

Your .env file should be part of the deployment environment and not part of your code base.

ie, your test server or your test server deployment script should have the settings for running your application on test instance.

The same for production, the production server or deployment script should make sure the production environment is setup correctly.

There is a good reason why these files are dot files - its to keep them out of your code repository. Don't try to find ways around it, think how you can attach the right environment to the right server install.

1 like
davidmarsmalow's avatar

@Snapey Thanks for the reply and insight. I understand, but I think currently I am misunderstanding about the difference between .env file and /config. Both of these file feels very similar to me and now I understand why Laravel and any other framework separate these two.

My mistake is that I should write application config inside /config wether create a new file or just adding new to existing. For example i could save Payment Status code and desc (which are application level config) inside /config/global_variable.php. And for server configuration I should save it inside the .env file (ex: database host, etc) and grab this value from .env file in the /config.

martinbean's avatar

@davidmarsmalow Those settings should be set as environment variables proper on each server, and not in .env files. Your code should be completely independent of any configuration.

1 like
lorenzoenrico.gigliotti's avatar

There's the method useEnvironmentPath() in Illuminate\Foundation\Application. I did something like this in my last project: bootstrap/app.php

//define('APP_ENV', 'local');
//define('APP_ENV', 'staging');
//define('APP_ENV', 'prod');
define('APP_ENV', 'home');
if(APP_ENV === 'home){
 $app = Application::configure(basePath: dirname(__DIR__))
->withRouting(...)
.
.
.
->create();
$app->useEnvironmentPath('environments/home');
return $app;

Just create a folder environments with a sunfolder for any .env you want to load

Obviously keep the folder out of any commit and don't use this method in production

Please or to participate in this conversation.