If you’ve been working with Laravel, you’ve probably used env() to pull values from your .env file. You’ve also seen config() being use...
If you’ve been working with Laravel, you’ve probably used env()
to pull values from your .env
file. You’ve also seen config()
being used. But do you know when to use which? Picking the wrong one can lead to unexpected issues—especially in production.
At first, it might not seem like a big deal. But trust me, if you get this wrong, you could end up debugging for hours, wondering why your .env
keys aren’t working. I learned this the after taking a hit. I was integrating WhatsApp API into a Laravel project. Everything was working smoothly on my local machine and testing server — I could send and receive messages without any issues. But as soon as I deployed to production… My API calls were failing with "Invalid token" errors.
After digging through and testing, I found the issue: I was using env()
directly in my code.
.env
Values Can Be Overridden in ProductionEnvironments like Docker, CI/CD pipelines, or FrankenPHP, may set system-level environment variables that override your .env
keys. That’s what happened to me—my WhatsApp API key was overridden by an external variable and my app never picked up the correct value. It was picking null
from the system environment.
When you run php artisan config:cache
or when the app APP_ENV
is set to production, Laravel loads all config values once and stores them in a cache file. After that, it never looks at .env
again until you clear the cache.
So if you call env('WHATSAPP_API_KEY')
in your application code and later update your .env
file, your app won’t see the new value until you clear the config cache.
Imagine you need to rename a key and env('WHATSAPP_API_KEY')
is scattered throughout your codebase; you now have to find and update every single instance manually. Worse, if something stops working, you’ll be searching through your entire project to figure out why the value isn’t being read correctly.
If you ever face issues where Laravel is not reading .env
values, env()
is returning null, or .env
changes aren’t reflecting in your app, this is exactly why.
The right way to manage your configuration can save you from a lot of frustration. Instead of calling env()
in your application code, store all environment-dependent values in Laravel’s config files.
Here’s how you do it:
Create a config file (or update an existing one) in the config
folder and use env()
here. I was integrating WhatsApp, so I created a file config/whatsapp.php
:
return [
'api_key' => env('WHATSAPP_API_KEY'),
'webhook_secret' => env('WHATSAPP_WEBHOOK_SECRET'),
];
Store values in your .env
file:
WHATSAPP_API_KEY=your_api_key
WHATSAPP_WEBHOOK_SECRET=your_webhook_secret
Now, use config()
instead of env()
in your code:
$apiKey = config('whatsapp.api_key');
$webhookSecret = config('whatsapp.webhook_secret');
If you change any .env
values, refresh the config cache:
php artisan config:clear
If you need to rename a key, you wouldn’t have to update many files — just update config/whatsapp.php
and clear the cache.
env()
all over your codebase, stop right there!
.env
values, .env
returns null, or config cache causes unexpected behavior, switching to config()
will solve your problem.env()
in your app logic may break your app by system-level overrides.config()
instead — it’s faster, safer, and cache-friendly!Avoid debugging surprises when deploying to production. This small habit can save you hours of debugging, make your app more maintainable and let it work as expected in all environments—local, staging, and production. Now, go ahead and check your project. If you find env()
calls outside config files, refactor your code today and save yourself from trouble down the road.
Try it out, and thank me later. 😉 Happy coding! 🎉
Subscribe to our "Article Digest". We'll send you a list of the new articles, every week, month or quarter - your choice.
What do you think about this?
Wondering what our community has been up to?