syamsoul/laravel-set-env
Programmatically set, update, and read Laravel .env variables with a simple facade and CLI. Supports comments, precise placement, and multiple env files (.env, .env.example). Includes production safety checks for secure environment configuration management.
A Laravel package that enables programmatic modification of environment variables in your .env file. Simple, secure, and efficient way to manage your Laravel application's environment configuration.
You can install the package via Composer:
composer require syamsoul/laravel-set-env
First, import the Env facade in your code:
use SoulDoit\SetEnv\Facades\Env;
There are multiple ways to set or update environment variables:
// Set/update variable in .env file
Env::set("MY_APP_NAME", "My Laravel Application");
// Set/update variable in .env.example file
Env::envFile('.env.example')->set("MY_APP_NAME", "Localhost");
// Add a comment to explain the variable
Env::set("ENABLE_CLOCKWORK", false, "Enable or disable the clockwork debugging tools");
// Result in .env:
// ENABLE_CLOCKWORK=false #Enable or disable the clockwork debugging tools
// Add/update a key after a specific key
Env::set("ENABLE_CLOCKWORK", false, afterKey: "APP_NAME");
// Result in .env:
// APP_NAME=MyApp
// ENABLE_CLOCKWORK=false
php artisan souldoit:set-env
php artisan souldoit:set-env "MY_APP_NAME=My Laravel Application"
php artisan souldoit:set-env "MY_APP_NAME=Localhost" -E .env.example
# or
php artisan souldoit:set-env "MY_APP_NAME=Localhost" --env_file=.env.example
Retrieve environment variable values using the get method:
$value = Env::get("MY_APP_NAME");
When working in a production environment, you can use the --force flag to bypass confirmation prompts:
php artisan souldoit:set-env "MY_APP_NAME=Production App" --force
If you find this package helpful, consider supporting the development:
BNB (BSC) Donation Address:
0x364d8eA5E7a4ce97e89f7b2cb7198d6d5DFe0aCe
This package is open-sourced software licensed under the MIT License.
How can I help you explore Laravel packages today?