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:- Form Submission - Filament collects form data
- Validation - Data is validated against rules
- EnvManager Called - Updates .env file
- File Write - New values written to
.env - Config Refresh - Laravel config automatically updates
- Success Notification - User sees confirmation
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:- ✅ Comments
- ✅ Empty lines
- ✅ Order of keys
- ✅ Other unchanged values
Security Considerations
File Permissions
The.env file should have restricted permissions:
Never Commit to Version Control
Your.gitignore should include:
- 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:.env but you can specify others:
Integrating EnvManager in Your Code
Basic Usage
In Filament Pages
The Settings pages use EnvManager in theirsave() 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:.env.
When Using EnvManager
If config is cached, changes won’t take effect until you clear the cache: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:- Clear config cache:
php artisan config:clear - Check file permissions on
.env - Verify file was actually updated:
cat .env | grep KEY - 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
- General Settings - Using general application settings
- Social Authentication - OAuth provider configuration

