Home / Snippets / Laravel Check Current Environment
Laravel Check Current Environment cover

Laravel Check Current Environment

453

3 years ago

0 comments

To check the current application environment in the Laravel backend there are 3 ways. The 1st method is by using the environment helper method, the 2nd one is using the config method and 3rd method is using the Laravel App facade.

Method 1: Using environment() Helper Method
The second method is to use the env() helper method and you can write it like below
<?php

if (app()->environment('local')) {
    dd('the current environment is local');
}

When using this method you can pass in multiple conditions such as in "local" and "production" just by passion in the or operator.
<?php

if (app()->environment('local') || app()->environment('production')) {
    dd('the current environment is either local / production');
}

Method 2: Using config() Helper Method
Just like the first method you can check by accessing the config directly using the helper method available made by Laravel.
if (config('app.env') === 'local') {
    dd('the current environment is local');
}

if (config('app.env') === 'local || config('app.env') === 'production') {
    dd('the current environment is either local / production');
}

Method 3: Use App Facade
The 3rd method is using the App facade and the code is as follows.
<?php

if (App::environment('local')) {
    dd('the current environment is local');
}

if (App::environment(['local', 'staging'])) {
    dd('the current environment is either local or staging');
}

Do note that you can try playing around with the .env value for the APP_ENV to see the value change.
APP_ENV=local

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