Laravel 5.2 not reading env file

夙愿已清 提交于 2019-11-27 03:11:37
Gaurav Gupta

From the official Laravel 5.2 Upgrade Notes:

If you are using the config:cache command during deployment, you must make sure that you are only calling the env function from within your configuration files, and not from anywhere else in your application.

If you are calling env from within your application, it is strongly recommended you add proper configuration values to your configuration files and call env from that location instead, allowing you to convert your env calls to config calls.

Reference: https://laravel.com/docs/5.2/upgrade#upgrade-5.2.0

benjolly1989

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

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

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

run this:

php artisan config:clear
php artisan cache:clear

then
php artisan config:cache

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

I had the same issue on local environment, I resolved by

  1. php artisan config:clear
  2. php artisan config:cache
  3. and then cancelling php artisan serve command, and restart again.

I missed this in the upgrade instructions:

Add an env configuration option to your app.php configuration file that looks like the following: 'env' => env('APP_ENV', 'production')

Adding this line got the local .env file to be read in correctly.

The simplicity is the power:

php artisan config:cache

You will receive:

Configuration cache cleared!

Configuration cached successfully!

delete cache using:

    php artisan config:clear
    php artisan config:cache

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.

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

For me the following worked

  • php artisan config:cache
  • php artisan config:clear
  • php artisan cache:clear

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
...

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!

Angel Prieto

I solved this problem generating a new key using the command: php artisan key:generate

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.

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.

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.

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

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!

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.

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!
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!