Simple and powerful ENV file editor for your Laravel application.
- PHP 8.2, 8.3, 8.4, or 8.5
- Laravel 11, 12, or 13
You can install the package via composer:
composer require amdadulhaq/env-editor-laraveluse AmdadulHaq\EnvEditor\EnvEditor;
// Set a new key or update an existing one
EnvEditor::set('APP_NAME', 'MyApp');
// Update an existing key (throws exception if key doesn't exist)
EnvEditor::update('APP_NAME', 'MyApp');
// Set or update (smart - checks if key exists)
EnvEditor::setOrUpdate('APP_NAME', 'MyApp');
// Remove a key
EnvEditor::remove('APP_NAME');// Get a single value
$appName = EnvEditor::get('APP_NAME');
$appName = EnvEditor::get('NON_EXISTENT_KEY', 'default_value'); // with default
// Get all values as an array
$allEnv = EnvEditor::getAll();
// Check if a key exists
$hasKey = EnvEditor::has('APP_NAME');// Create a backup (auto-generates timestamp-based filename)
$backupPath = EnvEditor::backup();
// Create a backup with custom name
$backupPath = EnvEditor::backup('backup-before-deployment.env');
// List all available backups
$backups = EnvEditor::listBackups();
// Restore from a backup
EnvEditor::restore('backup-before-deployment.env');use AmdadulHaq\EnvEditor\EnvEditor;
class SomeService
{
public function __construct(
protected EnvEditor $envEditor
) {}
public function updateSettings()
{
$this->envEditor->set('SOME_KEY', 'value');
$value = $this->envEditor->get('SOME_KEY');
}
}If you need to work with a different .env file:
$editor = new EnvEditor('/path/to/custom/.env');
$editor->set('KEY', 'value');- ✅ Read, write, and update .env file values
- ✅ Check if keys exist
- ✅ Get all environment variables as an array
- ✅ Backup and restore .env files
- ✅ Handle values with spaces, quotes, and special characters
- ✅ Dependency injection support
- ✅ Custom .env file path support
- ✅ Comprehensive error handling
- ✅ Full test coverage with Pest
composer testPlease see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.