Laravel Valet Environment Variables

To set/override Environment Variables in Laravel Valet, one had to manually edit the Nginx config files and restart Nginx after doing so. With the release of Laravel Valet 2.1.6 this is no longer needed: Valet 2.1.6 contains a merged PR that provides built-in support for an specific file named .valet-env.php in which you can set your environment variables.

🕸 Running an older version of Valet? Run these on the CLI to update:

# update package
composer global update

# Make Valet do its housekeeping
valet install

To set environment variables in Valet 2.1.6 or newer, create a file named .valet-env.php inside the directory where you ran valet link app-name before. Its contents must return an array with the envvars you want to define.

However, you must group these per app-name (e.g. the one you used during the valet link command), or you can use * as the wildcard. Each app-name define contains an array in itself, with the keys representing the names of the environment variable, and the values their respective value.

Here’s a few examples:

<?php

return [
	'*' => [ // Applies to all
		'APP_ENV' => 'dev',
	],
];
<?php

return [
	'app-name' => [ // Only applies to app-name.test
		'APP_ENV' => 'dev',
	],
];

It’s possible to combine * and app-name, their defined envvars will get merged at runtime:

<?php

return [
	'*' => [ // Applies to all
		'APP_ENV' => 'dev',
	],
	'myproject' => [ // Only applies to myproject.test
		'DB_NAME' => 'db_devdata',
	],
	'empty.myproject' => [ // Only applies to empty.myproject.test
		'DB_NAME' => 'db_empty',
	],
];

Here’s to no more fiddling with ~/.config/valet/Nginx/app-name.test files 🍻

Did this help you out? Like what you see?
Consider donating.

I don’t run ads on my blog nor do I do this for profit. A donation however would always put a smile on my face though. Thanks!

☕️ Buy me a Coffee ($3)

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.