boomdraw/laravel-dotenv

Laravel dotenv manager
3,055
Install
composer require boomdraw/laravel-dotenv
Latest Version:v2.1.0
PHP:^7.3|^8.0
License:MIT
Last Updated:Feb 11, 2021
Links: GitHub  ·  Packagist
Maintainer: boomdraw

Laravel Dotenv Manager

The package that allows read and write .env file variables

Installation

Via Composer

composer require boomdraw/laravel-dotenv

The package will automatically register itself.

You can publish the config file with:

php artisan vendor:publish --provider="Boomdraw\Dotenv\DotenvServiceProvider" --tag="config"

Usage and methods

use Dotenv;
//or
use Boomdraw\Dotenv\Facades\Dotenv;
//or
use Boomdraw\Dotenv\Contracts\DotenvContract;

class Controller
{
    /**
     * @var \Boomdraw\Dotenv\Repositories\DotenvRepository
     */
    protected $dotenv;

    public function __construct(DotenvContract $dotenv)
    {
        $this->dotenv = $dotenv;
    }
}

all

Dotenv::all(): Collection

The function returns all .env vars as an \Illuminate\Support\Collection object.

set

Dotenv::set($key, ?string $value = null): self

The function writes the .env variable regardless of the variable existence.

Dotenv::setEmpty($key, ?string $value = null): self

The function writes the .env variable if it does not exist or empty.

add

Dotenv::add($key, ?string $value = null): self

The function adds the .env variable if does not exist.

put

Dotenv::put($key, ?string $value = null): self

The function updates .env variable if it exists.

Dotenv::putEmpty($key, ?string $value = null): self

The function updates .env variable if it exists and empty.

delete

Dotenv::delete($key): self

The function deletes .env variable.

An array of keys to delete can be passed as $key variable.

Setters features

You can pass data as an array for setters (set, add, put):

Dotenv::set['key1' => 'value1', 'key2' => 'value2']

Setters transform variable name removing quotes (', "), replacing spaces ( ) and hyphens (-) with an underscore (_) and transforming name to uppercase.

For example Dotenv::set('foo bar', 'baz') will write FOO_BAR=baz to .env file.

All setters and delete will rewrite .env file immediately.

reload

Dotenv::reload(): self

The function reloads .env file from the filesystem.

Collection methods

You can call all of Collection methods. For example:

Dotenv::get('APP_NAME') returns APP_NAME value.

Dotenv::has('APP_NAME') checks APP_NAME variable existence.

All collection methods are called for Dotenv collection copy, so any changes to collection will not affect .env file and Dotenv content.

Testing

You can run the tests with:

composer test

Security

If you discover any security related issues, please email pkgsecurity@boomdraw.com instead of using the issue tracker.

License

MIT

Related Packages

davidcochrum/dotenv-vault-laravel

Laravel package to load environment variables from encrypted .env.vault files

3,248 1
alexhackney/laravel-doppler

Render a Laravel .env from Doppler secrets safely, atomically, and with a round-...

1,818 0
voopite/lumen-environment-editor

Simple .env file editor tool for Lumen microservices

10 0
lionix/envclient

Laravel environment client and console commands.

21,668 16

Version History

Version Released PHP Laravel License
v2.1.0 ^7.3|^8.0 ^8.0 MIT
v2.0.0 ^7.3|^8.0 ^8.0 MIT
v1.1.0 ^7.2 5.7.*|5.8.*|^6.0 MIT
v1.0.1 ~5.7|~5.8 MIT
v1.0.0 ~5.7|~5.8 MIT