If you have some experience with Linux, you are probably quite familiar with environment variables. In Linux, you could check the available environment variables with the
printenv command. A way to define environment variables in Linux would be to use the
export command followed by the variable that you want to define, for example:
In this tutorial, you will learn a couple of ways of defining Laravel variables and accessing them in your application!
Before you start, you would need to have a Laravel application up and running.
I will be using a DigitalOcean Ubuntu Droplet for this demo. If you wish, you can use my affiliate code to get free $100 DigitalOcean credit to spin up your own servers:
If you do not have that yet, you can follow the steps from this tutorial on how to do that:
Or you could use this awesome script to do the installation:
Defining a variable in
.env file holds your env variables for your current environment. It is powered by the DotEnv Library.
.env file often holds sensitive information like API keys or database credentials, you should never commit it to Git and never push it to GitHub.
In order to define a new environment variable, you could use the following syntax:
VAR_NAME=YOUR_VALUE. For example, let's say that you wanted to define your GitHub API key in there, the syntax would be the following:
In case that you have spaces in the value, it is best to use quotes:
Accessing the env variable with
Once you've specified your environment variable in your
.env file, you could then retrieve it in your application with the following helper function:
env helper function allows you to pass a default value in case that the env variable is not defined or does not have a value specified in the
In the above example, if the
NAME env variable is not defined, it will take the default value of
Accessing the env variable with
If you are creating a new Laravel Package or if you are building some specific functionality, it is pretty handy to define different configuration values in a configuration file stored in the
For example, let's say that we are building functionality that consumes the DigitalOcean API. What we can do is create a new file inside the
config folder called
The content of the fill will be a single return statement that returns an array with different env configuration values:
return [ 'do_key' => env('DIGITALOCEAN_API_KEY', 'default_value_here'), ];
With that we would also need to define the actual value of the
DIGITALOCEAN_API_KEY variable inside the
.env file just as we did in the previous step:
After that, by using the
config helper method, you could access the
do_key value with the following syntax:
Rundown of the syntax:
config(''): First, we call the config helper
- Then, we specify the name of the configuration file inside the
configfolder. As we named the file
digitalocean.phpwe need to just use
- Finally separated with a dot we specify the value that we want to call
With that, you could also use the
php artisan config:cache command to cache your env variables and give your website a slight speed boost.
Important thing to keep in mind is that if you run the
config:cachecommand, you will only be able to use the
envhelper inside configuration files in the
configfolder. And also, you need to have the
php artisan config:cachecommand executed on every deployment.
Now you know how to define custom variables in your Laravel application! For more information about the Laravel configuration, make sure to check out the official documentation here:
If you are just getting started with Laravel, make sure to check out this introduction course here: