This package allows you to programmatically set environment variable into .env
file.
- Laravel 10.x (and above)
This package can be used in Laravel 9.x or higher. If you are using an older version of Laravel, there's might be some problem. If there's any problem, you can create new issue and I will fix it as soon as possible.
You can install the package via composer:
composer require syamsoul/laravel-set-env
First, you must add this line to import Env
service.
use SoulDoit\SetEnv\Env;
To set/update environment variable in .env
file, just simply use the set
method.
$envService = new Env();
$envService->set("MY_APP_NAME", "My Laravel Application");
// or set variable in .env.example file
$envService = new Env('.env.example');
$envService->set("MY_APP_NAME", "Localhost");
or you can set/update the environment variable via Artisan
command.
php artisan souldoit:set-env
or
php artisan souldoit:set-env "MY_APP_NAME=My Laravel Application"
or, set variable in .env.example file
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
- When using --force option, it will bypass confirmation prompts in production.
php artisan souldoit:set-env "MY_APP_NAME=Production App" --force
To get environment variable in .env
file, just simply use the get
method.
$envService = new Env();
$the_value = $envService->get("MY_APP_NAME");
// $the_value will return `My Laravel Application`
If you find this package helps you, kindly support me by donating some BNB (BSC) to the address below.
0x364d8eA5E7a4ce97e89f7b2cb7198d6d5DFe0aCe
The MIT License (MIT). Please see License File for more information.