Posts Learn Components Snippets Categories Tags Tools About
/

How to check and access environment in Laravel Blade view?

Learn how to check-and-access the environment in Laravel blade view the easy way

Created on Aug 15, 2021

297 views

You can access the Laravel environment from the blade views by using several of the provided directive and methods.

Method 1: Using @env Directive
The 1st method to access and check for the current environment is using the @env directive. This method is the most straightforward way you can define it like below.
@env('staging')
    <p>You are currently in staging environment</p>
@endenv

To check for multiple conditions, you can pass it as an array.
@env(['staging', 'production'])
    <p>You are currently in staging / production environment</p>
@endenv

Method 2: Using env() Helper Method
The second method is to use the env() helper method and you can write it like below
@if (app()->environment('local'))
    <p>You are currently in local environment</p>
@endif

When using this method you can pass in multiple conditions such as in "local" and "production" just by passion in the or operator.
@if (app()->environment('local') || app()->environment('production'))
    <p>You are currently in local / production environment</p>
@endif

Method 3: Using config() Helper Method
Just like method 2 you can access the config directly using the helper method from the blade template.
@if (config('app.env') === 'local')
    <p>You are currently in local environment</p>
@endif

@if (config('app.env') === 'local || config('app.env') === 'production')
    <p>You are currently in local / production environment</p>
@endif

Other Read

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

)