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

redcore's avatar

Accessing Custom Environment Variable

Getting a variable is pretty straightforward, and it works fine (Config::get('app.key')) but when I try to create a custom variable in .env, like app.whatever it brings up nothing.

What I've tried:

  1. Changing the value of a standard Laravel var, like app.key - works fine. So, cache is not the issue, but...
  2. I did try clearing the cache with php artisan cache:clear - no bueno.
  3. print_r($_ENV) shows app.whatever in the array, so it's there.
  4. Thinking maybe 'app' is reserved, I tried to name the variable simply whatever - still not able to get it.

Should I just be using $_ENV['APP_WHATEVER']? That feels...dirty. And that's okay, I can do dirty, but I try to keep it out of my code if at all possible ;) I'm still pretty new and I think I've Googled everything I can think of and checked the docs, but I may be missing something simple.

Thanks guys :)

0 likes
8 replies
sylar's avatar
sylar
Best Answer
Level 31

env('app.whatever', 'default')

2 likes
bobbybouwmann's avatar

Maybe it's because of the dot? I would say keep the laravel convention in the .env file

SMTP_HOST=someString
SMTP_USERNAME=someString
SMTP_PASSWORD=someString
MANDRILL_KEY=someString
env('SMTP_HOST', 'someString');
env('SMTP_USERNAME', 'someString');
env('SMTP_PASSWORD', 'someString');
env('MANDRILL_KEY', 'someString');
1 like
constb's avatar

@redcore are you trying to access values from .env using $_ENV? that's not how it works. Use env() helper function or better - use .env only to set values in config, this way they will be cached when you run php artisan config:cache in production environment and you will avoid some performance loss there.

redcore's avatar

@constb - "that's not how it works." I wouldn't have made the topic if I felt that was the right way to go about it :|

thanks @sylar :) I figured it was something simple.

VictorAssis's avatar

I just went through the same situation but in case I left the variable as APP_FRONT_URL in the .env file and in the config/app.php folder I added a new index in the array returning the environment variable:

'front_url' => env('APP_FRONT_URL', 'http://localhost'),

And to call the value I use:

config('app.url')
1 like
liamvictor's avatar

I find that for my own variables I have to refer to them in the following format in blade: {{ env('GTM_ID', 'GTM-XXXXXXX') }}.

Having defined in the .env file LVD_QUOTE='All Earth in One Union' only the first version works in the blade.

(Laravel 5.5)

            {{ env('LVD_QUOTE', 'LVD_QUOTE') }}

            {{ env('lvd_quote', 'lvd_quote') }}

            {{ env('LVD.QUOTE', 'LVD.QUOTE') }}

            {{ env('lvd.quote', 'lvd.quote') }}



            {{ config('LVD_QUOTE', 'LVD_QUOTE') }}

            {{ config('lvd_quote', 'lvd_quote') }}

            {{ config('LVD.QUOTE', 'LVD.QUOTE') }}

            {{ config('lvd.quote', 'lvd.quote') }}
rameezisrar's avatar

How can you create a new instance of a class based on .env value?

DEFAULT_EMAIL_SERVICE=SendGrid

Now I would like to create an instance of whatever class marked as default.

Please or to participate in this conversation.