Posts Learn Components Snippets Categories Tags Tools new About
/

Laravel Check Current Environment

Learn how to check the current application environment form the backend code to perform certain logic whether in local, staging, or production environment

Created on Aug 15, 2021

205 views

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

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

Load comments for Laravel Check Current Environment

)