Home / Snippets / How to check and access environment in Laravel Blade view?
How to check and access environment in Laravel Blade view? cover

How to check and access environment in Laravel Blade view?

631

3 years ago

0 comments

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
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

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

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this