Using Variables in Your .env File

Using Variables in Your .env File

Laravel’s .env file is included to use, so it’s easy to have a different configuration based on the environment your app is running on. This gives you the flexibility to have different variables for local, staging, production, and even different developers’ machines.

Typically these files are small and manageable, but there are times when you run into situations where you duplicate the same data within the file. Here is an example:

MAIL_USERNAME=hello@laravel-news.com
MAIL_FROM_ADDRESS=hello@laravel-news.com

The dotenv package that Laravel relies on can use variables with other defined variables in this same file. For example:

MAIL_USERNAME=hello@laravel-news.com
MAIL_FROM_ADDRESS=${MAIL_USERNAME}

Granted this file isn’t usually crazy long or complicated, but this simple trick allows you not to repeat yourself and can be useful when you have multiple services requiring the same piece of data.

Many thanks to Caleb Porzio for pointing out the ability to do this.

Filed in: Laravel Tutorials / env
Laravel News Partners

Laravel Jobs

Senior Full Stack Engineer - Laravel
Remote
Givebutter
Laravel partner seeks Lead Developer
Remote
Kirschbaum Development Group
Senior Programmer **Immediate Opening** (send resume to daniel@sbgcorp.com)
Remote
SBG Corp
Web Developer - Laravel
Grand Forks, North Dakota
Center for Rural Health
Senior Full-Stack Laravel Developer
Berlin, Germany
felmo.de

Newsletter

Join 31,000+ others and never miss out on new tips, tutorials, and more.