dimer47 / laravel-env-migrations
Versioned migration system for Laravel's .env file, similar to database migrations.
1.0.1
2026-03-17 23:27 UTC
Requires
- php: ^8.2
- illuminate/console: ^11.0|^12.0
- illuminate/database: ^11.0|^12.0
- illuminate/filesystem: ^11.0|^12.0
- illuminate/support: ^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^9.0|^10.0
- phpunit/phpunit: ^11.0
README
Versioned migration system for Laravel's
.envfile, similar to database migrations. ποΈ
π«π· Lire en franΓ§ais
π Features
- π Versioned migrations β manage
.envchanges just like database migrations - π¦ Database tracking β tracking table with batches and JSON history
- βͺ Rollback β revert the last batch of changes
- π Status β view the state of all migrations
- π§© Laravel ready β auto-discovery, publishable config, Artisan commands
- π‘οΈ Production safe β mandatory confirmation in production (unless
--force) - π§ 11 methods β set, get, remove, rename, copy, group, block, etc.
π Installation
Via Composer (packagist)
composer require dimer47/laravel-env-migrations
π§ͺ Via a local repository (development)
Add to the host project's composer.json:
{
"repositories": [
{
"type": "path",
"url": "packages/laravel-env-migrations"
}
],
"require": {
"dimer47/laravel-env-migrations": "*"
}
}
Then:
composer update dimer47/laravel-env-migrations
π§ Configuration
Publish the package files:
# Publish the config php artisan vendor:publish --tag=env-migrations-config # Publish the tracking table migration php artisan vendor:publish --tag=env-migrations-migrations # Run the migration php artisan migrate
βοΈ Configuration options (config/env-migrations.php)
| Option | Description | Default |
|---|---|---|
table |
Name of the tracking table | env_migrations |
path |
Directory for migration files | database/env-migrations |
π Quick Start
β Create a migration
php artisan env:migrate:make add_redis_configuration
This creates a file in database/env-migrations/ with a ready-to-use template.
βοΈ Write a migration
<?php use Dimer47\EnvMigrations\EnvMigration; return new class extends EnvMigration { public function getDescription(): string { return 'Configure Redis for cache and queues'; } public function up(): void { // Set a variable $this->set('CACHE_DRIVER', 'redis'); // Set only if missing $this->setIfMissing('REDIS_HOST', '127.0.0.1'); // Rename a variable (keeps its position) $this->rename('OLD_REDIS_PORT', 'REDIS_PORT'); // Copy a variable $this->copy('REDIS_HOST', 'REDIS_CACHE_HOST'); // Remove a variable $this->remove('DEPRECATED_CACHE_KEY'); // Group variables together $this->ensureGroup([ 'REDIS_HOST' => '127.0.0.1', 'REDIS_PASSWORD' => 'null', 'REDIS_PORT' => '6379', ], '# Redis Configuration'); // Insert a group after a reference variable $this->ensureGroupAfter('CACHE_DRIVER', [ 'CACHE_PREFIX' => 'myapp', 'CACHE_TTL' => '3600', ]); // Append a multi-line block $this->appendBlock(<<<'ENV' # Queue Configuration QUEUE_CONNECTION=redis QUEUE_RETRY_AFTER=90 ENV, 'QUEUE_CONNECTION'); } public function down(): void { $this->set('CACHE_DRIVER', 'file'); $this->remove('REDIS_CACHE_HOST'); $this->rename('REDIS_PORT', 'OLD_REDIS_PORT'); $this->removeBlock('# Queue Configuration', 'QUEUE_RETRY_AFTER'); } };
βΆοΈ Run migrations
php artisan env:migrate
php artisan env:migrate --force # Skip confirmation in production
π View status
php artisan env:migrate:status
βͺ Rollback last batch
php artisan env:migrate:rollback
php artisan env:migrate:rollback --force # Skip confirmation in production
π Available methods
| Method | Description |
|---|---|
set($key, $value) |
Set or update a variable |
get($key) |
Read a variable's value |
remove($key) |
Remove a variable |
rename($oldKey, $newKey) |
Rename a variable (keeps its position) |
copy($source, $target) |
Copy a variable to a new key |
exists($key) |
Check if a variable exists |
setIfMissing($key, $value) |
Set only if not already defined |
ensureGroup($vars, $comment) |
Group variables together |
ensureGroupAfter($afterKey, $vars, $comment) |
Insert a group after a reference variable |
appendBlock($block, $identifier) |
Append a multi-line block (duplicate-safe) |
removeBlock($start, $end) |
Remove a block by markers |
π₯οΈ Artisan Commands
| Command | Description |
|---|---|
env:migrate |
βΆοΈ Run pending migrations |
env:migrate:rollback |
βͺ Rollback the last batch |
env:migrate:status |
π Display migrations status |
env:migrate:make {name} |
β Create a new migration |
ποΈ Tracking table schema
| Column | Type | Description |
|---|---|---|
id |
BIGINT |
Auto-incremented primary key |
migration |
VARCHAR(255) |
Unique migration name |
batch |
INT |
Execution batch number |
changes |
JSON |
Detailed change history |
executed_at |
TIMESTAMP |
Execution date |
π License
MIT β See LICENSE