Skip to content

Env variable setter for Laravel (programatically set/update variable in .env file)

Notifications You must be signed in to change notification settings

syamsoul/laravel-set-env

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Env Variable Setter for Laravel

Latest Version on Packagist

 

Introduction

This package allows you to programmatically set environment variable into .env file.

 

   

Requirement

  • Laravel 10.x (and above)

   

Installation

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

   

Usage

First, you must add this line to import Env service.

use SoulDoit\SetEnv\Env;

 

Set New Variable or Update Existing Variable

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

Force option in production

  • When using --force option, it will bypass confirmation prompts in production.
php artisan souldoit:set-env "MY_APP_NAME=Production App" --force

 

Get Variable's Value

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`

   

Support me

If you find this package helps you, kindly support me by donating some BNB (BSC) to the address below.

0x364d8eA5E7a4ce97e89f7b2cb7198d6d5DFe0aCe

   

License

The MIT License (MIT). Please see License File for more information.

About

Env variable setter for Laravel (programatically set/update variable in .env file)

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages