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

guerrilla_power's avatar

How can I access environment in view?

Hi,

I want to show text based on the Laravel environment. How to do that?

0 likes
4 replies
bobbybouwmann's avatar
Level 88

You have a few options here. The best way to do this is using your config file. However you can also ask Laravel directly what the current environment is.

The config file is based on the config set in your .env and you can access it like so

// view
@if (config('app.env') === 'production')
    Production text
@endif

Using Laravel directly you can do something like this

// view
@if (app()->environment() === 'production')
    Production text
@endif

// Or you can do this
@if (app()->environment('production'))
    Production text
@endif

I prefer to use the config option, however with the app()->environment('production') you can directly check on the correct environment.

6 likes
guerrilla_power's avatar

Thanks! This is exactly what I was looking for and it works right away!. This forum is amazing!!!

afridi's avatar

You can also use this method env() for accessing any environmental variable. For example if you want to access APP_ENV variable from .env file, you should call it like this way; env('APP_ENV') this will output the value of this variable set in env file or in config

Please or to participate in this conversation.