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

zoransa's avatar

L5 .env how to put test env in the same folder

Earlier we just could make .env.local.php .env.test.php and trsting scripts would pick up testing environment where scripts truncate tables and do havoc while on local I would actually like to preserve data that we pull from backup of production database.

0 likes
16 replies
xingfucoder's avatar

Hi @bart, according with this question I have the following:

If you need to use an .env general file but you also need an .env testing file for Codeception testing environment. What would be the steps needed to create a testing environment? In the last version of Laravel you could create local folders for Local environment and leave the general config/app.php, database.php for production environment.

Thanks in advanced.

alidbc's avatar

I appreciate if someone can shed some light on this. I remember when Jeff was working on behat integration he mentioned load environment from file function.

kfirba's avatar

You can easily achieve that by doing:

$app->loadEnvironmentFrom('.env.test');

// or
App::loadEnvironmentFrom('.env.test');
2 likes
kfirba's avatar

@codeatbusiness Depends what you are trying to achieve. If it's for testing purposes, you can do that before the tests you call that method.

Mattiman's avatar

It's still not clear where I should put that code. I tried:

class ExampleTest extends DbTestCase {

    public function setUp()
    {
        parent::setUp();
        App::loadEnvironmentFrom('.env.test');
   }
}

But then I get:

......PHP Fatal error:  Call to undefined method Illuminate\Foundation\Application::loadEnvironmentFrom() in /home/vagrant/Code/myproject/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 210
xingfucoder's avatar

I having the same problem, I'd like to create a test .env file and use for testing purposes while I maintain the general .env file. I don't know if is possible that.

bart's avatar

Well, that's a question I'm asking myself currently, too. The new DotEnv conecept is simple, but the question is, if it is able to handle same variables for different environments, like it did before. Maybe you have to replace the .env file for testing and re-replace it by the original one afterwards. @JeffreyWay could you bring some light into the darkness, please?

atan's avatar

Another question. In L4 I can set different app.php to use different providers/aliases array for develop and production environment. But how to do this in L5 .env

zoransa's avatar

This is exactly what I was trying to find out. For behat package is loading default App::loadEnvironmentFrom('.env.test'); if we do phpspec, unit testing I would like to create few env.beta env.mama env.behat env.jeffrey

JeffreyWay's avatar

You could check the $_SERVER keys and then use $app->loadEnvironmentFrom() accordingly. You'll want to do this logic early, so maybe put it in bootstrap/app.php.

1 like
rapliandras's avatar

Kind of relevant topic so thought I'd mention that cascaded config files only work now if every single file is present, and they won't fall back to the main configuration in config/. It causes weird errors trying to migrate from a previous laravel dev version, so maybe the best solution here is to eliminate cascaded config folders for distinct environments entirely. I didn't read anything about this before, so it was surprising to see as I liked the old one better.

lucknerjb's avatar

To expand on @JeffreyWay's comment, right after the app is created in app.php, you want to put something along the lines of this:

// Detect env
$testServers = ['YOUR LOCAL URL'];
if (isset($_SERVER['HTTP_HOST']) && in_array($_SERVER['HTTP_HOST'], $testServers)) {
   $app->loadEnvironmentFrom('.env.testing');
}

Same if you want to emulate production or something like that.

anand-moghe's avatar

If I have to use API to check the environment (like in AWS you can check for instance tag) and if I add the code to load specific .env file (for development, test, prod etc) using loadEnvironmentFrom as per @JeffreyWay's comment above, then what caching scheme can I use here in bootstrap/app.php? So that I do not have to run the code for every request? I want to minimize operation of running the api to detect my environment in bootstrap/app.php. And as per my knowledge I will not be able to use the session or other facade in bootstrap/app.php.

Please or to participate in this conversation.