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?

560

2 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

Ahmad Aziz

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 ☕️

Post Tutorials

Learn trending tutorials for ReactJs, React Native, VueJS, NextJs, Laravel, Tailwind CSS, and Flutter development.
View Tutorials

Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that's right for your web projects
View Components

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this