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:

$envFilePath = base_path('.env');

// Check if the .env file exists
if (file_exists($envFilePath)) {
// Open the file for reading
$envFile = fopen($envFilePath, 'r');

// Read each line and echo the contents
while (($line = fgets($envFile)) !== false) {
echo $line; // Output each line with a line break for readability
}

// Close the file after reading
fclose($envFile);
} else {
echo "The .env file does not exist.";
}

echo "FINALLY:" . env('APP_URL') . PHP_EOL;

$envFilePath = base_path('.env');

// Check if the .env file exists
if (file_exists($envFilePath)) {
// Open the file for reading
$envFile = fopen($envFilePath, 'r');

// Read each line and echo the contents
while (($line = fgets($envFile)) !== false) {
echo $line; // Output each line with a line break for readability
}

// Close the file after reading
fclose($envFile);
} else {
echo "The .env file does not exist.";
}

echo "FINALLY:" . env('APP_URL') . PHP_EOL;
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:
Ok so, just add this to the config/services.php file ```php ‘sharepoint’ => [ ‘tenant_id’ => env(‘SHAREPOINT_TENANT_ID’),...
Jump to solution
11 Replies
Miguel García
Did you try clearing config cache?
Dennis Koch
Dennis Koch6d ago
You chould never use env() directly. It should only be used in config files Otherwise this might be a OPCache issue
jjo63
jjo636d ago
yes, 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.
awcodes
awcodes5d ago
Was app url just an example? Because that’s already available via the congfig() helper or the Config facade.
jjo63
jjo635d ago
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.
env('SHAREPOINT_TENANT_ID')
env('SHAREPOINT_TENANT_ID')
Solution
awcodes
awcodes5d ago
Ok so, just add this to the config/services.php file
‘sharepoint’ => [
‘tenant_id’ => env(‘SHAREPOINT_TENANT_ID’),
… other values
]
‘sharepoint’ => [
‘tenant_id’ => env(‘SHAREPOINT_TENANT_ID’),
… other values
]
awcodes
awcodes5d ago
Then you can access them with
config(‘services.sharepoint.tenant_id’)
config(‘services.sharepoint.tenant_id’)
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.
Dennis Koch
Dennis Koch5d ago
It's mentioned in the env() docs: https://laravel.com/docs/11.x/helpers#method-env
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.
jjo63
jjo635d ago
Thanks for the info, really appreciate it. Thanks Dennis - I guess mine stopped working after I executed a
php artisan config:cache
php artisan config:cache
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() )?
Dennis Koch
Dennis Koch5d ago
I'd expect clearing the config to work, but apparently it didnt'?
jjo63
jjo635d ago
Looks like I may have been in a muddle - I can confirm that, as of now, my test script which contains the line
echo('Value:' . env('MAIL_FROM_ADDRESS') . PHP_EOL);
echo('Value:' . env('MAIL_FROM_ADDRESS') . PHP_EOL);
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
Want results from more Discord servers?
Add your server