Check for the application environment in Laravel views

Amit Merchant · August 5, 2020 ·

Often you’d come across a scenario where you’d like to check the application environment and based on that you’d like to render things.

For instance, you’d like to check if the environment application currently running in is “production”, you can verify it in the view like so.

    {{-- in "production" environment --}}

Now, this is alright that we’re leveraging the App::environment method to identify the environment of the application and it even works fine but it’s kind of verbose as you can see.

To fix this, Laravel provided some in-built Blade directives to check for the environment. Here’s how the previous code would translate to if we use Laravel’s in-built Blade directive.

    {{-- in "production" environment --}}

As you can see, it’s pretty straight-forward now. All you need to do is mention the environment name like @environment and end it like @endenvironment so.

If you want to check for multiple environments in a single conditional, you can do it like so.

@env('local', 'staging')
    {{-- in "local" or "staging" environment --}}

Hi there! I'm Amit. I write articles about all things web development. If you like what I write and want me to continue doing the same, I would like you buy me some coffees. I'd highly appreciate that. Cheers!