Laravel uses an .env file to configure environment-specific variables for the application. For example URL, database connection, queues, Redis connection, external services, etc. You can add your own environment variables and use them in your code.
In this short article we will explain how to get the env variable in Laravel in the blade or controller, and how to do it properly.
How to Get ENV Variable in Laravel
Laravel has the env() helper that allows you to get the value for an variable from the .env file. You may think that you can use it everywhere. But it is a bad idea. Let’s do a little experiment. Open the .env file, and add any variable with a value. For example SOME_VARIABLE:
CUSTOM_VARIABLE="haait"

Next, try to display the value of the variable in code using the env() helper:
dd(env('CUSTOM_VARIABLE');

It works as expected. Now, run this command to cache Laravel configuration:
php artisan cache:config
And try to display the value of our variable again:
dd(env('CUSTOM_VARIABLE');

You will get null. Why? Because the env() helper is intended to be used only for populating configuration files in the config directory. You shouldn’t use it in another code, for example in controllers or blade files. So, when the config cache is enabled env() helper will always return null. This can lead to a hard-to-detect problem when everything works locally, but not in a production environment.
So, if you need any env variable in the controller or blade, define it in any config file and extract the value using the config() helper. Here is a description of several Laravel configuration files which you can use:
- app.php – general application settings;
- auth.php – authorization and authentication settings;
- database.php – database and data storage settings;
- filesystems.php – file system configuration;
- services.php – file for external services configuration.
In 90% cases I use the services.php file to place my environment variables or create a dedicated file. Let’s add our variable to this file:
return [
//...
"custom_variable" => env("CUSTOM_VARIABLE"),
//...
];

Now, you can get it from the controller using this code:
\config("services.custom_variable");

Don’t forget to update the config cache or clear it after changes. Now, you know how to use the env variable Laravel in the controller. If you want to get it in the blade files, use this code:
@php
config('services.some_variable')
@endphp
Or use the Config facade:
@php
Config::get('services.some_variable')
@endphp
You know how to read the env variable Laravel from blade files or controllers. Let’s have a look at how to get the current environment. Also you may want to create custom helper in Laravel to extract a needed value.
How to Check the Current Environment in Laravel
The environment is not present in the config files. But you can use the environment() method instead of using the env(‘APP_ENV’) helper. As the first parameter, this method envisages a string with the environment name or a list of environments and will return true if the current environment is matched with the one from the list. For example, you can check is the environment development or staging using this code:
app()->environment('development', 'staging')

There is no specific helper for blade files, so you should wrap the code using @php or other condition directive and it will work.
Wrapping Up
In this short article we have explained how to get env variable in Laravel through configuration. As you see it is pretty simple and can make Laravel faster because it uses the cache and doesn’t parse configuration files every time.