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)

Join the Conversation

2 Comments

  1. Hey,
    thanks for this post.
    One thing I don’t get though, why is there even a wildcard option, or the possibility to set project name keys, if the .valet-env.php is ONLY read if you drop it into a project’s root…?

    I have project A: here I drop the env file, and set

    return [
    
      // global ENV vars
    	'*' => [
    		'Blah' => 'Blubb',
    	],
    
    ];

    But this is then available only for project A anyways, not for project B. Or am I misunderstanding something here?

    I’m trying to figure out how (or if) I can set global ENV vars for all projects, so that I don’t have to install a third-party php extension (Maxmind). As 99% of my projects are using these…

    1. The .valet-env.php file is meant to be used on a per project-basis, not for all your projects.

      When you run valet link projectname, it will be exposed through the URL https://projectname.test/. Additionally, it is also reachable through any subdomain of projectname.test … and that’s where the support for multiple hostnames comes into play. See the last example in the post:

      • When visiting the project using https://myproject.test, it will use the database that contains dev data
      • When visiting the project using https://empty.myproject.test, it will use a database that is empty
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.