PHP code example of amdadulhaq / env-editor-laravel

1. Go to this page and download the library: Download amdadulhaq/env-editor-laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

amdadulhaq / env-editor-laravel example snippets


use 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');
    }
}

$editor = new EnvEditor('/path/to/custom/.env');
$editor->set('KEY', 'value');