How To Store and Access Information using .env File – Laravel?

How To Store and Access Information using .env File – Laravel

Almost all web applications today have some amount of sensitive information that as a developer, we should not be adding directly in the code or commit to our source control.

Laravel provides a very neat way of keeping all the sensitive information of a Laravel app away from the code via its configuration file, also known as the .env file.

The .env file already contains keys for the most commonly needed tasks such as the database details, the SMTP server details, etc.

However, there are times when we need access to sensitive information that doesn’t have any relevant entry in the default .env files. How can we add custom key-value pairs to our applications .env file?

How To Add Custom Config Keys in a .env file?

Adding custom fields / key-value pairs to the .env file is a 2-step but simple process. Let’s see how it works with the help of an example. Let’s assume I have to add the following two fields in my application’s .env file and subsequently, I want to use the values against these fields, in the application itself.


I want to use the above-mentioned fields’ values in my application to do some tasks.

Step 1 – Create a config file and add config entries to the file

Go to the App\Config and create a new PHP file. For this example, we will call it payu_config.php.

In the newly-created file, add the following code:


    'auth_header'        =>       env('PAYU_AUTH_HEADER'),
    'auth_key'           =>       env('PAYU_AUTH_KEY'),

What we did above is, we first created a config file that will have our config data in it, and then, we added the two configuration entries that will read data from the .env file.

At this moment, it still does not do anything and is not usable but soon it will be.

Step 2 – Create Entries in the .env file

Open your .env file that is in the root folder of your project. Scroll to the bottom of the file and type in the following two lines:


You must replace yourr_auth_header and your_auth_key with whatever valid and relevant value is applicable in your case. In my case, I had a valid auth header and auth key that was provided by PayUMoney to me.

Once you have typed in the correct values and saved and closed your .env file, you also must restart your web server (in case you are already running it) for the changes to take effect.

In order to access the values you stored in the .env file, you will have to do the following in your controller:

$header_value_from_env_file = config('payu_config.auth_header');

$key_value_from_env_file = config('payu_config.auth_key');

In case, your code above doesn’t pull in expected values after the server restart, you might also clear the cache.

You can clear the cache of your Laravel project by running the following command:

php artisan co:cache

Now, if you want to have a quick look back at what we have done here, you will realize that all we are actually doing is just the following simple steps:

  1. We create new entries in the config file and assign values to them.
  2. We then create a new config file under the config directory in our project folder.
  3. From within this config file, we access the values stored in the .env file.
  4. And finally, we use the values in our config file fields (that we pull from the .env file) in our controller code.

That’s it!

Share This Post

3 thoughts on “How To Store and Access Information using .env File – Laravel?”

  1. Pingback: Add Google Rich Snippets on Product Pages on a Laravel Website - Rajiv Verma

  2. Pingback: How To Create a Responsive Grid Layout Using TailwindCSS? - Rajiv Verma

  3. Pingback: How To Import Data From Excel (XLS, XLSX) Files in Laravel? - Rajiv Verma

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe To my Future Posts

Get notified whenever I post something new

More To Explore