How to check current laravel environment value?

The Production and local are just environment names in your Laravel applications that you can use to turn certain testing features on or off in different places. But to know the current active environment there are a few tricks/codes for this. Which we will see further in this article. But before that, there are a few things you have to be aware of for each environment type.

You can set your app for the local environment to the local mode by using this setting:

APP_ENV=local

For the Production mode

APP_ENV=production

There are multiple ways to check this current environment type:
The current application environment is determined via the APP_ENV variable from your .env file. You may access this value via the environment method on the App facade:

use Illuminate\Support\Facades\App;

$environment = App::environment();

Both are same

$environment = app()->environment();
OR
$environment = App::environment();

If you want then you may also pass the arguments to the environment method to determine if the current application environment matches the given value. The method will return true/false if the environment matches any of the given values:

if (App::environment('local')) {
    // The environment is local
}
 
if (App::environment(['local', 'staging'])) {
    // The environment is either local OR staging...
}

To check the environment in terminal/command prompt for the current framework is running:

php artisan --env
OR
php artisan env

To get environment variable value or return default value:

$env = env('APP_ENV');
$env = env('APP_ENV', 'production');

Do not forget to run the command php artisan config:clear after you have made the changes to the .env file.
To get to know more about the Laravel, you can check these articles too.

Please follow and like us:

Related Posts

Leave a Reply

Share