Laravel not reading changes to .env file
When upgrading from Laravel 5.1 to 5.2, a common regression is discovering that your .env variables are no longer being read, even after following the official upgrade guide. If you are seeing errors like Access denied for user 'forge'@'localhost', it usually indicates that the framework is falling back to default configuration values instead of pulling from your environment file. This issue can cripple applications because third-party packages, such as Bugsnag or queue drivers, often rely on these same environment variables to function correctly.
The root cause lies in how Laravel initializes its container and handles configuration caching. In earlier versions, the framework was more lenient with loading the .env file at runtime. However, modern PHP environments, particularly those running under specific SAPIs like FPM or CLI, require explicit handling of environment variables before they are accessible to the application code.
The $_ENV Reality Check
The diagnostic step revealed in the source material is critical: running php artisan tinker shows that env('DB_DATABASE') returns null and dd($_ENV) returns an empty array. This confirms that the variables exist in your file system but are not loaded into the process's memory space at runtime.
Laravel does not automatically load the .env file when you start the server or CLI. Instead, it expects the environment to be set up via the bootstrap/app.php bootstrap file or a specific artisan command. If you simply copy a fresh Laravel 5.2 instance into your app folder without modifying the bootstrap process, the framework will never read the .env file on its own.
To resolve this, you must ensure that the .env file is loaded during the bootstrapping phase. The standard approach involves adding a call to Env::load() early in the bootstrap sequence. In Laravel 5.2 and later, this is often handled automatically by the composer install process if the _ENV array is not empty, but explicit loading is safer.
Add the following line at the very top of your bootstrap/app.php file:
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
// Load environment variables from .env file
$app->bind('env', function ($app) {
$app->loadDotEnv();
});
Alternatively, and more commonly in 5.2 setups, you can use the config package which automatically loads the .env file if it exists. However, if that fails, manually invoking the loader is necessary.
Clearing Caches
Once the loading mechanism is confirmed or fixed, you must clear the configuration cache. Laravel caches your configuration files to improve performance, but this cache can become stale after a deployment or an upgrade. Run the following commands via your terminal:
php artisan config:clear
php artisan cache:clear
These commands ensure that Laravel re-reads config/database.php and retrieves the correct values from the environment variables rather than relying on cached defaults. If you are using a production server, remember to run these as part of your deployment script before restarting the web server or PHP processes.
Why This Happens in Upgrades
The transition from 5.1 to 5.2 introduced stricter requirements for environment isolation. In 5.1, the framework could often infer environment values even if they weren't explicitly loaded early in the boot process. By 5.2, the expectation is that $_ENV or $_SERVER are populated before any configuration class is instantiated.
If you are using a shared hosting environment or a specific PHP-FPM setup where the .env file permissions are too restrictive, the web server might not be able to read the file at all. Ensure your .env file has permissions set to 600 or 644, and that only your application user can read it.
Final Verification
After applying the changes, verify that the environment variables are accessible within Tinker:
php artisan tinker
>>> config('database.connections.mysql.database')
=> 'mydb'
If this returns your expected database name instead of 'forge', the issue is resolved. The key takeaway is that Laravel 5.2 requires explicit bootstrapping of environment variables, and relying on implicit loading can lead to persistent configuration errors across all your config files.