Skip to content

tarfin-labs/laravel-config

Repository files navigation

Laravel Config Logo

Latest Version on Packagist GitHub Workflow Status Quality Score Total Downloads

Introduction

Laravel config provides a simple configuration system for your Laravel application.

Installation

You can install the package via composer:

composer require tarfin-labs/laravel-config

Next, you should publish the Laravel config migration file using the vendor:publish Artisan command.

php artisan vendor:publish --provider="TarfinLabs\LaravelConfig\LaravelConfigServiceProvider" --tag="laravel-config"

If you want to use Laravel Config database factory, you can publish it too, using the command:

php artisan vendor:publish --provider="TarfinLabs\LaravelConfig\LaravelConfigServiceProvider" --tag="laravel-config-factories"

Finally, you should run your database migrations:

php artisan migrate

Documentation

Simple usage example of laravel-config package in your Laravel app.

Create new config parameter:

$factory = new ConfigFactory();
$configItem = $factory->setName('key')
    ->setType(ConfigDataType::BOOLEAN)
    ->setValue('1')
    ->setTags(['system'])//optional
    ->setDescription('Lorem ipsum dolor sit amet')
    ->get();

LaravelConfig::create($configItem);

Get value with config name:

LaravelConfig::get('key');

Set value with config name and value:

LaravelConfig::set('key', 'value');

Get all config parameters:

LaravelConfig::all();

Get config items by tag:

LaravelConfig::getByTag('key');

Check if the config exists:

LaravelConfig::has('key');

Update config with new values:

$factory = new ConfigFactory($configId);
$configItem = $factory->setName('updated-key')
    ->setType(ConfigDataType::BOOLEAN)
    ->setValue('0')
    ->setTags(['system'])//optional
    ->setDescription('updated description')
    ->get();

LaravelConfig::update($configItem);

Remove config:

LaravelConfig::delete('key');

Nested Parameters

Let's say you have a config parameters named foo.bar and foo.baz. You can get all parameters under foo namespace using getNested() method.

Usage:

LaravelConfig::getNested('foo');

Output: Illuminate\Support\Collection

=> Illuminate\Support\Collection {#3048
     all: [
       TarfinLabs\LaravelConfig\Config\Config {#3097
         id: 1,
         name: "bar",
         type: "boolean",
         val: "0",
         description: null,
         created_at: "2021-05-06 11:35:05",
         updated_at: "2021-05-06 11:35:05",
       },
       TarfinLabs\LaravelConfig\Config\Config {#3099
         id: 2,
         name: "baz",
         type: "boolean",
         val: "1",
         description: null,
         created_at: "2021-05-06 11:03:48",
         updated_at: "2021-05-06 11:03:48",
       },
     ],
   }

Helpers

You can also use helper functions:

// Creating config item
$factory = new ConfigFactory();
$configItem = $factory->setName('key')
    ->setType(ConfigDataType::BOOLEAN)
    ->setValue('1')
    ->setTags(['system'])//optional
    ->setDescription('Lorem ipsum dolor sit amet')
    ->get();

create_config($configItem);

// Reading config item
read_config('key');

// Checking if the config item exists
has_config('key');

// Shortcut to update the value of config item
set_config_value('key', 'value');

// Updating config item
$factory = new ConfigFactory($configId);
$configItem = $factory->setName('updated-key')
    ->setType(ConfigDataType::BOOLEAN)
    ->setTags(['system'])//optional
    ->setValue('0')
    ->setDescription('updated description')
    ->get();

update_config($configItem);

// Removing config item
delete_config('key');

// Reading nested config items
read_nested('foo.bar');

Custom Casters

You can also custom casters:

$config = factory(Config::class)->create([
    'name' => 'custom-cast-config-name',
    'val' => [ConfigDataType::DATE],
    'type' => AsEnumCollection::class.':'.ConfigDataType::class,
]);

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

Laravel config is open-sourced software licensed under the MIT license.