tarfin-labs / laravel-config
Key value config management for Laravel
Installs: 12 875
Dependents: 0
Suggesters: 0
Security: 0
Stars: 16
Watchers: 8
Forks: 3
Open Issues: 4
Requires
- php: ^8.1|^8.2|^8.3
- ext-json: *
- illuminate/support: ^8.49|^9.0|^10.0|^11.0
- laravel/legacy-factories: ^1.0
Requires (Dev)
- orchestra/testbench: ^6.13|^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.5.2
- dev-master
- 5.1.1
- v5.1.0
- v5.0.2
- v5.0.1
- v5.0.0
- v4.7.0
- 4.6.0
- 4.5.0
- 4.4.0
- 4.3.0
- 4.2.0
- 4.1.0
- 4.0.0
- v3.0.0
- 1.2.0
- 1.1.0
- 1.0.0
- dev-laravel-11-upgrade
- dev-php-8-2-support
- dev-laravel-10-upgrade
- dev-laravel-9-support
- dev-tag-feature
- dev-php8-support
- dev-nested-config-params
- dev-laravel-8-compatibility
- dev-adding-helpers-to-document
- dev-laravel-7-upgrade
- dev-adding-helper
- dev-publishing-factory
This package is auto-updated.
Last update: 2024-11-02 07:48:06 UTC
README
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 development@tarfin.com instead of using the issue tracker.
Credits
License
Laravel config is open-sourced software licensed under the MIT license.