Skip to main content

Overview

Larafast Multi-Tenancy uses a custom EnvManager system that allows administrators to update environment variables directly from the admin panel. This guide explains how it works and how you can leverage it.

How It Works

The EnvManager Class

Location: app/Helpers/EnvManager.php This helper class provides methods to read, write, and delete environment variables in your .env file safely.

Value Handling

The EnvManager intelligently handles different value types:

Booleans

Strings with Spaces

Strings with Special Characters

Numbers

Null Values

How Updates Work

When you click “Save Changes” in the Settings pages:
  1. Form Submission - Filament collects form data
  2. Validation - Data is validated against rules
  3. EnvManager Called - Updates .env file
  4. File Write - New values written to .env
  5. Config Refresh - Laravel config automatically updates
  6. Success Notification - User sees confirmation
No Server Restart Required! Changes take effect immediately for new requests.

The .env File

File Location

Your environment file is located at the project root:

File Format

The .env file uses key=value pairs:

Updating Mechanism

The EnvManager reads the entire file, updates the specific key(s), and writes it back:
Preserves:
  • ✅ Comments
  • ✅ Empty lines
  • ✅ Order of keys
  • ✅ Other unchanged values

Security Considerations

File Permissions

The .env file should have restricted permissions:
This ensures only the application owner can read/write it.

Never Commit to Version Control

Your .gitignore should include:
Why?
  • Contains sensitive credentials
  • API keys and secrets
  • Database passwords
  • OAuth secrets

Backup Before Changes

The EnvManager doesn’t automatically backup. Consider adding a backup step:

Environment-Specific Files

Use different files for different environments:
Laravel automatically loads .env but you can specify others:

Integrating EnvManager in Your Code

Basic Usage

In Filament Pages

The Settings pages use EnvManager in their save() method:

In Controllers

In Artisan Commands

Advanced Scenarios

Conditional Updates

Only update if value changed:

Bulk Updates with Validation

Restoring from Backup

Configuration Cache

How Laravel Caches Config

Laravel can cache configuration for performance:
This creates a single file with all configuration, ignoring .env.

When Using EnvManager

If config is cached, changes won’t take effect until you clear the cache:
Recommendation: Don’t cache config in development. Only cache in production after ensuring environment is stable.

Checking if Config is Cached

Best Practices

1. Validate Before Saving

Always validate environment values before writing:

2. Use Transactions for Critical Updates

3. Log Changes

Track who changed what:

4. Provide Rollback Mechanism

5. Clear Caches After Updates

Troubleshooting

Changes Not Taking Effect

Solutions:
  1. Clear config cache: php artisan config:clear
  2. Check file permissions on .env
  3. Verify file was actually updated: cat .env | grep KEY
  4. Restart queue workers: php artisan queue:restart

File Permission Errors

Error: “Permission denied when writing to .env” Solution:

Values Not Escaped Properly

Problem: Values with special characters breaking Solution: EnvManager handles this automatically, but verify:

Concurrent Updates

Problem: Multiple admins updating settings simultaneously Solution: Use locks:

Next Steps