Outsource Laravel Development Partner - $3200/Month | Bacancy

Keep Your Environment Files Updated with Envy by Worksome

Published on by

Keep Your Environment Files Updated with Envy by Worksome image

We've all been there. You're getting a new developer up and running with your Laravel project, and for some reason, it's nothing but 500 errors! It works on your machine... what on earth is going on? You've checked all of the usual suspects; the database exists and migrations have been run.

Hours pass. Confusion mounts. Muttering ensues.

Then suddenly, you facepalm. There's a missing entry in their .env file. See, more often than not, we forget to keep our .env.example file up to date because it no longer has any meaningful impact on our development cycle. It's only when this happens that you swear you're going to update it. Perhaps you do, perhaps you don't. But even if you do, likely it will get outdated again.

At Worksome, we got tired of this endless cycle of pain. So we decided to do something about it! Meet Envy, the Laravel package that will make sure you never miss an environment variable again.

How does it work?

Envy is not a sync-up between your .env and .env.example file. See, that has a lot of pitfalls. For starters, you almost never want to actually copy environment values from your .env, because they tend to contain sensitive information that shouldn't be in version control. Additionally, if you no longer need an environment variable in your project because you delete a config file or remove a package, a simple sync between these two files won't help remove those now outdated variables.

Envy is much smarter. It reads all of your project's config files (or any other files you've configured) and finds calls to the env function. Then, it compares those calls to what's contained inside your .env.example file. With that knowledge, it is able to suggest additions or removals for you, so that syncing your environment file with your project is only ever an Artisan command away.

Envy has two Artisan commands: php artisan envy:sync and php artisan envy:prune. Sync is designed to insert missing environment variables into your .env.example file. Prune is designed to remove environment variables that are no longer required.

Don't worry, neither of these commands will make changes without confirmation (unless you pass the --force option).

Running in CI

Of course, you'd still have to run these commands to sync everything up, which you may forget to do. To make sure you never miss this again, both Artisan commands provide a --dry option that will check for required additions or removals and return a failure code if any are found.

Let's take a look at a basic GitHub workflow that would perform this check for you.

name: Check Environment Variables
 
on: [pull_request]
 
jobs:
envy:
runs-on: ubuntu-latest
 
steps:
- name: Checkout code
uses: actions/checkout@v2
 
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
 
- name: Install Composer dependencies
run: composer install --prefer-dist --no-interaction
 
- name: Run Envy sync
run: php artisan envy:sync --dry
 
- name: Run Envy prune
run: php artisan envy:prune --dry

Pretty straightforward stuff, hey? With this in place (obviously tweaked to your project's requirements), you'd know before any PR is merged if your .env.example file needs some TLC.

Configuration

Envy has tons of configuration options, all of which can be customised in config/envy.php. The Envy docs have extensive documentation on all of the options available to you. One thing I'd like to point out however, is the fact that you can add additional environment files to check. So, if you have a .env.testing or .env.dusk file, you could add those into the mix to ensure they remain up to date.

One thing to remember is that Envy will only search files in your config directory for env calls. This is because you should only use the env function in your config files, as using it elsewhere will prevent app config caching. However, if you need to add additional directories to search, you may add them here.

/**
* Here you should list any config files/directories that you want to be
* included when looking for calls to `env`. Directories are searched
* recursively. Feel free to include unpublished vendor configs too.
*/
'config_files' => [
config_path(),
base_path('vendor/spatie/laravel-permission/config'),
],

Side notes

I wanted to take a moment to say how much I enjoyed writing Envy. I built it using the action paradigm, which you can see my Laracon talk on here. It makes use of PHP AST for parsing and creating PHP files, which is always entertaining. It also marks my first time using the excellent Termwind package for styling console applications - you should definitely check it out. Writing HTML for the console is so much nicer and feels immediately familiar.

We put a lot of polish into this one, and we hope it comes through.

Conclusion

Envy is certainly going to help us keep things up to date, and we hope it can help you in your projects too! Go read the docs, check out the source code, and remember to like the project on GitHub if it helps you out!

Take care, Luke

Luke Downing photo

Full stack developer at Worksome. CEO of Downing Tech. Big fan of TDD, and maintainer of the Pest PHP testing framework.

Filed in:
Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
Livewire 4 Is Dropping Next Week, and wire:transition Makes Animations Effortless image

Livewire 4 Is Dropping Next Week, and wire:transition Makes Animations Effortless

Read article
Laravel 12.45.1, 12.45.2, and 12.46.0 Released image

Laravel 12.45.1, 12.45.2, and 12.46.0 Released

Read article
"Don't Remember" Form Helper Added in Inertia.js 2.3.7 image

"Don't Remember" Form Helper Added in Inertia.js 2.3.7

Read article
Laravel News Partners With Laracon India image

Laravel News Partners With Laracon India

Read article
A new beta of Laravel Wayfinder just dropped image

A new beta of Laravel Wayfinder just dropped

Read article
Ben Bjurstrom: Laravel is the best Vibecoding stack for 2026 image

Ben Bjurstrom: Laravel is the best Vibecoding stack for 2026

Read article