问题
After upgrading to Laravel 5.2, none of my .env file values are being read. I followed the upgrade instructions; none of my config files were changed except auth.php. They were all working fine in previous version, 5.1.19
.env contains values such as
DB_DATABASE=mydb
DB_USERNAME=myuser
config/database.php contains
\'mysql\' => [
\'database\' => env(\'DB_DATABASE\', \'forge\'),
\'username\' => env(\'DB_USERNAME\', \'forge\'),
]
I get this error:
PDOException: SQLSTATE[HY000] [1045] Access denied for user \'forge\'@\'localhost\' (using password: NO)
Clearly not pulling in my env config. This is affecting every single one of my config files, including third party such as bugsnag.
I also tried
php artisan config:clear
php artisan cache:clear
Update
Trying php artisan tinker
>>> env(\'DB_DATABASE\')
=> null
>>> getenv(\'DB_DATABASE\')
=> false
>>> config(\'database.connections.mysql.database\')
=> \"forge\"
>>> dd($_ENV)
[]
I have tried installing a fresh copy of Laravel 5.2. I basically only copied in my app folder; no additional composer packages are included. Still having the same issue. I have other Laravel 5.2 projects on the same server that are working fine.
回答1:
From the official Laravel 5.2 Upgrade Notes:
If you are using the
config:cachecommand during deployment, you must make sure that you are only calling theenvfunction from within your configuration files, and not from anywhere else in your application.If you are calling
envfrom within your application, it is strongly recommended you add proper configuration values to your configuration files and callenvfrom that location instead, allowing you to convert yourenvcalls toconfigcalls.
Reference: https://laravel.com/docs/5.2/upgrade#upgrade-5.2.0
回答2:
If any of your .env variables contains white space, make sure you wrap them in double-quotes. For example:
SITE_NAME="My website"
Don't forget to clear your cache before testing:
php artisan config:cache
php artisan config:clear
回答3:
Wow. Good grief. It's because I had an env value with a space in it, not surrounded by quotes
This
SITE_NAME=My website
Changed to this
SITE_NAME="My website"
Fixed it. I think this had to do with Laravel 5.2 now upgrading vlucas/phpdotenv from 1.1.1 to 2.1.0
回答4:
I had a similar issue in my config/services.php and I solved using config clear and optimize commands:
php artisan config:clear
php artisan optimize
回答5:
run this:
php artisan config:clear
php artisan cache:clear
thenphp artisan config:cache
回答6:
For me it has worked this in this order:
php artisan config:cache
php artisan config:clear
php artisan cache:clear
And I've tried all the rests without luck.
回答7:
I had the same issue on local environment, I resolved by
- php artisan config:clear
- php artisan config:cache
- and then cancelling php artisan serve command, and restart again.
回答8:
I missed this in the upgrade instructions:
Add an env configuration option to your
app.phpconfiguration file that looks like the following:'env' => env('APP_ENV', 'production')
Adding this line got the local .env file to be read in correctly.
回答9:
When you fired command php artisan config:cache then it will wipe out all the env variables and env() will give null values, try running follwing command and boom there your env() again begin to catch all env variable
php artisan config:clear
回答10:
For me the following worked
- php artisan config:cache
- php artisan config:clear
- php artisan cache:clear
回答11:
The simplicity is the power:
php artisan config:cache
You will receive:
Configuration cache cleared!
Configuration cached successfully!
回答12:
delete cache using:
php artisan config:clear
php artisan config:cache
回答13:
Same thing happens when :port is in your local .env
again the double quotes does the trick
APP_URL="http://localhost:8000"
and then
php artisan config:clear
回答14:
Also additional to what @andrewtweber suggested make sure that you don't have spaces between the KEY= and the value unless it is between quotes
.env file e.g.:
...
SITE_NAME= My website
MAIL_PORT= 587
MAIL_FROM_NAME= websitename
...
to:
...
SITE_NAME="My website"
MAIL_PORT=587
MAIL_FROM_NAME=websitename
...
回答15:
I ran into this same problem on my local, and I have tried all the answers here but to no avail. Only this worked for me,
php artisan config:clear and restart server. Works like a charm!
回答16:
I solved this problem generating a new key using the command: php artisan key:generate
回答17:
if you did call config:cache during local development, you can undo this by deleting the bootstrap/cache/config.php file. and this is work for me.
回答18:
if you did call config:cache during local development, you can undo this by deleting the bootstrap/cache/config.php file. and this is work for me.
@Payal Pandav has given the comment above.
I want to tell a simple workaround. Just edit the config.php file in the bootstrap/cache/ folder. And change the credentials. This worked for me. Please don't delete this file since this may contain other crucial data in the production environment.
回答19:
I experienced this. Reason was that apache(user www-data) could not read .env due to file permissions. So i changed the file permissions to ensure that the server (apache) had read permissions to the file. Just that and boom, it was all working now!
回答20:
In my case laravel 5.7 env('APP_URL') not work but config('app.url') works. If I add new variable to env and to config - it not works - but after php artisan config:cache it start works.
回答21:
If you run this php artisan config:cache command on console then it will store all the .env file contents in cache, after this command if you append any contents into .env file the it will not be not be available until you run php artisan config:clear command
回答22:
I made the mistake by doing dd/die/dump in the index.php file. This causes the system to not regenerate the configs.
Just do dump in view files will do. The changes to .env file update instantly.
回答23:
I face the same problem much time during larval development. some times env stop working and not return any value. that reason may be different that depends on your situation. but in my case a few days ago I just run
PHP artisan::config:clear
so be careful use of this command. because it will wipe all config data form its cache. so after that, it will not return any value. So in this situation, you need to use this first if you have run PHP artisan config:: clear command.
php artisan config:cache // it will cache all data
php artisan config:clear
Configuration cache cleared!
回答24:
Tried almost all of the above. Ended up doing
chmod 666 .env
which worked. This problem seems to keep cropping up on the app I inherited however, this most recent time was after adding a .env.testing. Running Laravel 5.8
来源:https://stackoverflow.com/questions/34420761/laravel-5-2-not-reading-env-file