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
Leave a reply