I'm randomly getting this error 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected' from laravel\framework\src\Illuminate\Database\Connection.php line 664. I think might be due to the missing variable, not able to read from .env Anyone encounter this issue? and managed to resolved it?
Just cancel serve and again serve the app and everything ok.
I have started to use env() only in configuration files (/config folder). And then config() in all other files. Which resulted in no more issues.
Sadly do not remember the source to this wisdom :|.
I face that error, in my case y was running laravel on apache and some queue workers with supervisor. I found that when the .env change I must reload the workers manually with supervisorctl restart all or the changes wont work. If you are using the cli (ie php artisan command) the .env file is only reloaded when you restart the cli process In production before any cli/process restart i use the following command as say in other comments:
php artisan config:cache
php artisan config:clear
php artisan cache:clear
Do you know of a fix for this?
This is an old issue, in Laravel 7.28.3 continue happening...
I watch two minutes ago my UI doesn't have an important part that depends on a env value. Yesterday it was OK. I is OK again after run:
php artisan config:clear
In docs mentioned ,
Once the configuration has been cached using php artisan config:cache , the .env file will not be loaded and all calls to the env function will return null.
So to load any custom variables from env file after config:cache command, those variables needs to declared in config/app.php file as
'custom_var' => env('CUSTOM_VAR',null)
after setting variable like above, we can access this variable even after running config:cache command also with config('app.custom_var') in entire application
@if , @pluzz , @crushcandy , @stormshadow , @john.pope , @cronix , @trevorg , @rmariuzzo , @kaylakaze , @gmehtaster , @skovmand , @igor_talevski , @rverrips , @rajakammara , @orici
Simply hit below commands -
php artisan config:clear
php artisan cache:clear
And you are done with this issue, hopefully. Otherwise, think about the god and start R & D again using this link .
config:clear solved my issue
For me, config('app.env') did the trick.
in config folder -> app.php:
'env' => env('APP_ENV', 'production'),
In .env.php on root path:
APP_ENV=local
So, .env file overwrites config file, in local enviroment config('app.env') gets "local" and in production, "production".
Still have the same issue with laravel 8.75
Solved it by removing the default variable from the config
'database' => env('DB_DATABASE'), 'username' => env('DB_USERNAME'), 'password' => env('DB_PASSWORD'),
I have faced the same issue but the solution which is working for me is to run php artisan config:clear command when I ran other cache commands like optimize, config:cache etc... the issue comes again but after ran this command config:clear my appication read env file properly.
@Ahmed-Ibrahim you needed to bump a 7 YEAR OLD THREAD to state what is in the docs ?
It's an oldie, but still happens in Laravel 10. I've managed to fix this by creating a variable inside config/app.php. For example, I wanted to reference the API_ENDPOINT env variable in my code, which was intermittently not working.
I added: 'api_endpoint' => env('API_ENDPOINT'), to the config/app.php file, which references the .env variable.
Why add the extra bounce you ask? This way, when you run config:cache, it caches the environment variable instead of requesting it from the .env each time it's needed.
Side note: If you run config:cache, the app will only load variables from the cached config and not your .env, so it's best practice to only reference variables from your config in your code, eg: config('app.api_endpoint'), not env('API_ENDPOINT').
Hope this helps :)
Ok, everything should be in config, but ¿Why disable the use of env(), despite the fact config() is being cached? I can't find an explanation to that. env() is env(), config() is config(), two sepparate worlds. Running things in k8s or docker depend on environment management, where env() has a significant relevance.
@carlosmora because config might be accessed many times, so pulling config into cache is a speed boost.
If it did not then ignore env values, it would have to merge the env values with the config at runtime - at which point the caching is no longer of use.
Why is it such an issue for you to take env values, bring them into config and then cache it?
Guys, in my case it was the silliest thing ever... in the .env file, I've introduced a new variable named APP_VERSION="1.0" but I wrote it using spaces APP_VERSION = "1.0" As a result of any of the above commands, I got the: "./.env: line 3: APP_VERSION: command not found" error. So, sharing this here as it may save someone time to figure it out.
Adding this here for a Production environment to hopefully save someone a headache tracking it down. Was getting the same issue intermittently, made all of the changes suggested in this thread and a couple other threads to no avail.
Setup: Bitbucket pipelines CICD, Debian 10, Laravel 10 only running CLI commands and Scheduled tasks running 60 supervisord workers.
Problem: Intermittently the logs would show DB Access denied messages with the 'forge' or other default users from the database config file.
Resolution: Problem ended up being the CICD, there was a small amount of time between the rsync that pushed files and the commands 'php artisan optimize:clear && php artisan config:cache && php artisan queue:restart', and the rsync was deleting the bootstrap/cache/config.php file that the config:cache command creates.
Final solution was to add an exclude argument to the rsync call such as this --exclude=bootstrap/cache/*
Please or to participate in this conversation.