Laravel allows you to use .env files to define environment-specific variables. For example, a project deployed locally and a project in production will have different database connection settings, caching and queue settings, and so on. Moreover, you can also add your custom variables to the.env file and then use them in your code.
However, it is not as straightforward as it looks. You shouldn’t access the variables directly from the .env file. In this article, I will explain why and show how to get the env variables in Laravel properly.
How to Get the ENV Variable in Laravel
You can use the env() helper to access variables defined in the .env file in Laravel. However, it is a bad idea to use this helper project-wide. There is a config folder in each Laravel project with several configuration files that contain the settings of various services and packages. To access the values of configuration parameters from this folder, you can use the config() helper. This configuration can be cached to increase the speed of the application, and then the env() helper stops working since it is intended only to fill in the configuration in the config folder.
Let’s do a little experiment. Open your .env file and add any new variables to it. For example, SOME_VARIABLE:
.envCUSTOM_VARIABLE="value"
Then, create a command and print the value of this variable in the handle() method:
php artisan make:command PrintCustomVariableCommand
app/Console/Commands/PrintCustomVariableCommand.php<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class PrintCustomVariableCommand extends Command
{
protected $signature = "app:print-custom-variable";
protected $description = "Prints a custom variable value";
public function handle()
{
\dump(\env("CUSTOM_VARIABLE"));
return self::SUCCESS;
}
}
php artisan app:print-custom-variable
As you can see, the value is displayed properly. Now let’s cache the configuration:
php artisan config:cache
Then, let’s try to get the value of this variable again:
php artisan app:print-custom-variable
This time, we will get null instead of the real value because, as I said above, when a configuration is cached, the env() helper always returns null. This can cause errors and bugs that will be difficult to track and fix later because your code will work locally but not on the server where caching is enabled.
So, to get the value of your variable in the controller class, you first need to assign its value to one of the configuration parameters in the config folder. You can add the parameter to one of the existing files or create a new one, for example, myconfig.php:
config/myconfig.php<?php
return [
"custom_variable" => \env("CUSTOM_VARIABLE", "default_value")
];
If you need to store settings for some external API or library, you can place them in the config/services.php file. In all other cases, I prefer to create separate files so that my settings don’t get mixed with Laravel’s settings and will not be lost on a framework update.
Before accessing the value of this variable, you should regenerate the configuration cache:
php artisan config:cache
And then use the config() function to pass the name of the file and the variable:
app/Console/Commands/PrintCustomVariableCommand.php<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class PrintCustomVariableCommand extends Command
{
protected $signature = "app:print-custom-variable";
protected $description = "Prints a custom variable value";
public function handle()
{
\dump(\config("myconfig.custom_variable"));
return self::SUCCESS;
}
}
Wrapping Up
In this short article, we’ve looked at how to get the env variable in Laravel. As you can see, it has its own peculiarities, but in general, it is quite simple.