Help! Unable to access variable values from .env
This is really bizarre. And perhaps more of a Laravel question now I think about it. But...
My Filament app is able to connect to a database and display e.g. the APP_NAME - so I know that the .env file is present and has been read by Laravel /Filament core code.
But - I cannot access the values from within e.g. a Widget.
I have this line of code
Log::info('APP_NAME:' . env('APP_NAME'));
and it results in output
[2024-10-17 11:45:50] local.INFO: APP_NAME:
Even though the APP_NAME is clearly displayed (and has a value in the .env file) by Filament.
All my code which is trying to access other variables (I was just using APP_NAME as an example) has "suddenly stopped working" - and I am thoroughly perplexed.
Here is a little routine that proves the existence of the file:
But the echo of env('APP_URL') at the end gives nothing. Here is the script's output
$ php test.php APP_NAME="HCAT DB SYSTEM" APP_ENV=local APP_KEY=base64:4xxxxxxxxxxxxxxxx= APP_DEBUG=true APP_URL=https://xxxxxxxxxxxx ... ... ... FINALLY:Any clues anyone?
Solution:Jump to solution
Ok so, just add this to the config/services.php file
```php
‘sharepoint’ => [
‘tenant_id’ => env(‘SHAREPOINT_TENANT_ID’),...
11 Replies
Did you try clearing config cache?
You chould never use
env()
directly. It should only be used in config files
Otherwise this might be a OPCache issueyes, did already but to no avail.
Oh - ok - then that's something I need to read up on - however it was working and then mysteriously stopped. I don't want to persevere with code that is flaky because its behaviour may vary - where would I read up on the "right way" to do this?
I have just read this:
If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded; therefore, the env function will only return external, system level environment variables.I am guessing this is what has caused my app to "suddenly stop working" Just trying to figure out how to revert things AND how to set my custom variables in a file in config directory.
Was app url just an example? Because that’s already available via the congfig() helper or the Config facade.
Yes it was just an example - I have added additional variables that I need - I added these to .env (thinking that was the correct approach)
SHAREPOINT_TENANT_ID="xxxxxx"
SHAREPOINT_CLIENT_ID="yyyyyy"
SHAREPOINT_CLIENT_SECRET="zzzzz"
Then I referred to these when I needed them using e.g. Solution
Ok so, just add this to the config/services.php file
Then you can access them with
Doesn’t have to be services though. That just makes sense to me. You can also create any file you want in the config directory and access it the same way.
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Thanks for the info, really appreciate it.
Thanks Dennis - I guess mine stopped working after I executed a
Would that be right? If so, is there a way to restore the behaviour (I recognise that what I had was a really bad implementation that wouldn't have survived being put into production! - but I'd like to understand if possible to revert so that I can work my way through the code, fixing these uses of env() )?
I'd expect clearing the config to work, but apparently it didnt'?
Looks like I may have been in a muddle - I can confirm that, as of now, my test script which contains the line
returned nothing.
I then executed
php artisan config :clear
and the script returned the value from the .env file
I then cached the config
php artisan config:cache
and the script returned nothing again
So the config:clear
and config:cache
are working as I think was expected (not by me as this is new to me 🙁 ) and I will proceed to modify all references of env()
with references to config()
which in turn will find the values from a sharepoint.php file that I'm adding into the config directory.
Thanks for your patience and help,
j