Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

autefrum's avatar

How to access values in .env after config:cache

I am storing my email config in the .env file, because it contains secret credentials.

I am also storing non-secret stuff like EMAIL_ADDRESS_NO_REPLY in .enc to keep the stuff together.

In my notifications, I currently use ->from(env('EMAIL_ADDRESS_NO_REPLY','[email protected]'); to set the sender address on outbound emails.

However, https://laravel.com/docs/5.6/deployment say I should not use env() in code, only in config files: "be sure that you are only calling the env function from within your configuration files."

In the configuration doucmentation (https://laravel.com/docs/5.6/configuration) it says

"All of the configuration files for the Laravel framework are stored in the config directory."

but the .env file is in the application's root not the config.

The config helper documentation says it can be "accessed using "dot" syntax, which includes the name of the file and the option you wish to access."

What do I use for 'the name of the file' in to retrieve a value from .env - a file NOT in the confi folder? Or is the 'single file' created by this caching still accessible using env()?

0 likes
3 replies
Cronix's avatar
  1. .env files contain variables
  2. config files read those variables
  3. Use config() helper, to access them.

You're missing #2, so create config file. Let's say it's /config/custom.php

custom.php should return an array, which accesses env (see the other config files)

return [
    'noreply' => env('EMAIL_ADDRESS_NO_REPLY')
];

Now, access them anywhere with the config helper. As you said, you use dot notation. Since we called the config file "custom", and the array key is called "noreply", it would be

$from_address = config('custom.noreply');
autefrum's avatar

So I need to introduce a new config file between my .env file and my app. OK.

Cronix's avatar

You don't have to create a new one, although it would be best. You could just add it to an existing one as well. I don't like adding custom env variables to existing (framework) config files.

Please or to participate in this conversation.