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

breda's avatar

Laravel 5 Environment Detection Not Working

Hey guys! I'am having troubles with detecting environments with Laravel 5.0.18. It's just like that Laravel isn't picking up the environment name I'am putting in the APP_KEY. Example : I have this in my .env file : APP_ENV=staging

But when I do

Route::get('/', function() { dd(env('APP_ENV')); });
// OR
Route::get('/', function() { dd(App::environment()); });

It always gives me local !

I'am really confused here! Any ideas ?

0 likes
15 replies
michaeldyrynda's avatar

Are you putting staging into APP_KEY or APP_ENV?

What happens if you try dd(App::environment())?

breda's avatar

No, I'am putting staging in APP_ENV, not in APP_KEY...

And Like I said, both dd(App::environment()) & dd(env('APP_ENV')) return local, while should be returning staging!

Kemito's avatar

Can you please show us .env file (make sure you remove all important information, passwords, api keys)

breda's avatar

@devinfd Nope. Not using Homestead. Just serving the application with artisan serve.

@Kemito You got it! :)

APP_ENV=staging
APP_DEBUG=true
APP_KEY=RANDOM_STRING

DB_HOST=localhost
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
Kemito's avatar

For debug purpose please make test route. In that route just

\Log::info($_SERVER);

and check if there is APP_ENV there as well as what is value there. If variable is incorrect there aswell then i guess there is problem with booting. Please check your filename if that is correctly '.env' Is APP_ENV only env variable that doesnt work? Create test variable there "TEST_ME" for example and see log/ouput.

Hope this helps.

1 like
pmall's avatar
pmall
Best Answer
Level 56

@breda

Just serving the application with artisan serve.

When using php dev server, make sure you restart the server when you change the env file. I almost gone crazy the other day figuring this out.

8 likes
breda's avatar

Yeah @pmall ! I guess I did what you did! Gone crazy because of this! lol !

Thank you! As well as @Kemito ! Very appreciated :)

lukaserat's avatar

I am experiencing the same issue. Sharing what I did, hoping it will help. What I did is I remove the bootstrap/cache/config.php. Then all work out smoothly. This happens when you forgot that you cache your configuration. :) You can do the same using php artisan config:clear

6 likes
olimorris's avatar

Thanks all. Boy was this driving me crazy.

A quick php artisan config:clear and a restart of Homestead sorted it.

2 likes
ms08's avatar

I also have problem same too, When I " echo app()->environment() " , it's local. But when I debug:

$app = Config::get('app.cipher'); print_r($app);

Laravel get config of file app.php outside directory local.

This is in file .env

APP_ENV=local

APP_DEBUG=true

APP_KEY=my_key

.....

What's I am wrong ?

mstearne's avatar
php artisan config:clear

Worked for me.

1 like
encodia's avatar

I'm using Homestead. I set APP_ENV=staging,

If I run php artisan env:

  • on the host: I got the correct environment ("staging")
  • on the guest: I got the wrong environment ("local")

I tried cache:clear, config:clear, tried to reboot Homestead, but didn't solve. Any idea?

Thank you

rob897's avatar

If you are using Homestead as your Dev environment, no matter what you put in your your .env file seems to matter as your Homestead.yaml file has a APP_ENV key that seems to override what you use in your environment file.

1 like

Please or to participate in this conversation.