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

lara123's avatar

can't get getenv('key');

iam set key in .env file but can not get this variable name in a controller why ?

0 likes
7 replies
bashy's avatar

Use config() instead like so;

config('broadcasting.connections.pusher.key');
mushood's avatar

@lara123 Try running: php artisan config:clear

if it does not work, try: env('key')

Also as explained by @bashy, you use use config() as it is the recommended way.

Then you should run php artisan config:cache

bashy's avatar

@mushood Yeah shouldn't have to run config:cache on local. I'm sure there's somewhere where env() doesn't work as well... in constructors?

mushood's avatar

@bashy Thanks. I learnt something new here.

I googled and got this answer: https://stackoverflow.com/questions/29729543/how-to-stop-laravel-5-from-caching-configurations

@lara123, it might be relevant to your case. Especially that comment: "If you did however call config:cache during local development, you can undo this by deleting the bootstrap/cache/config.php file. Otherwise, you might experience that calling getenv() will not return the desired values – Sven P Mar 11 '16 at 8:50"

lara123's avatar

php artisan config:clear // env composer dump autoload php artisan cache:clear

Please or to participate in this conversation.