PHP code example of tarfin-labs / laravel-config

1. Go to this page and download the library: Download tarfin-labs/laravel-config library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tarfin-labs / laravel-config example snippets


use TarfinLabs\LaravelConfig\Facades\LaravelConfig;
use TarfinLabs\LaravelConfig\Config\ConfigFactory;
use TarfinLabs\LaravelConfig\Enums\ConfigDataType;

// Create a config parameter
$configItem = (new ConfigFactory())
    ->setName('app.debug')
    ->setType(ConfigDataType::BOOLEAN)
    ->setValue(true)
    ->get();

LaravelConfig::create($configItem);

// Get a config value
$debug = LaravelConfig::get('app.debug'); // returns true (boolean)

// Update a config value
LaravelConfig::set('app.debug', false);

// Check if config exists
if (LaravelConfig::has('app.debug')) {
    // ...
}

use TarfinLabs\LaravelConfig\Facades\LaravelConfig;

// Get a single config value
LaravelConfig::get('key');
LaravelConfig::get('key', 'default'); // with default value

// Set a config value
LaravelConfig::set('key', 'value');

// Check existence
LaravelConfig::has('key');

// Get all configs
LaravelConfig::all();

use TarfinLabs\LaravelConfig\Config\ConfigFactory;
use TarfinLabs\LaravelConfig\Enums\ConfigDataType;

$configItem = (new ConfigFactory())
    ->setName('mail.host')
    ->setType(ConfigDataType::STRING)
    ->setValue('smtp.example.com')
    ->setDescription('SMTP server hostname')
    ->setTags(['mail', 'system'])
    ->get();

LaravelConfig::create($configItem);

use Illuminate\Database\Eloquent\Casts\AsCollection;
use Illuminate\Database\Eloquent\Casts\AsEnumCollection;

$configItem = (new ConfigFactory())
    ->setName('allowed.statuses')
    ->setType(AsEnumCollection::class . ':' . StatusEnum::class)
    ->setValue([StatusEnum::Active, StatusEnum::Pending])
    ->get();

// Create configs with tags
$dbHost = (new ConfigFactory())
    ->setName('db.host')
    ->setValue('localhost')
    ->setTags(['database', 'connection'])
    ->get();

$dbPort = (new ConfigFactory())
    ->setName('db.port')
    ->setValue('3306')
    ->setTags(['database', 'connection'])
    ->get();

LaravelConfig::create($dbHost);
LaravelConfig::create($dbPort);

// Get all configs with a specific tag
$databaseConfigs = LaravelConfig::getByTag('database');

// Get configs matching multiple tags
$connectionConfigs = LaravelConfig::getByTag(['database', 'connection']);

// Create nested configs
LaravelConfig::create((new ConfigFactory())->setName('mail.host')->setValue('smtp.example.com')->get());
LaravelConfig::create((new ConfigFactory())->setName('mail.port')->setValue('587')->get());
LaravelConfig::create((new ConfigFactory())->setName('mail.encryption')->setValue('tls')->get());

// Get all configs under 'mail' namespace
$mailConfigs = LaravelConfig::getNested('mail');

// Returns a Collection with normalized names:
// [
//     ConfigItem { name: 'host', val: 'smtp.example.com' },
//     ConfigItem { name: 'port', val: '587' },
//     ConfigItem { name: 'encryption', val: 'tls' },
// ]

use TarfinLabs\LaravelConfig\Config\Config;

// Update using set() for simple value changes
LaravelConfig::set('app.name', 'New App Name');

// Full update with ConfigFactory
$config = Config::where('name', 'app.name')->first();
$configItem = (new ConfigFactory($config))
    ->setValue('Updated Name')
    ->setDescription('Updated description')
    ->get();

LaravelConfig::update($config, $configItem);

// Delete a config
$config = Config::where('name', 'obsolete.config')->first();
LaravelConfig::delete($config);

// Create
create_config($configItem);

// Read
read_config('key');           // Get single value
read_config();                // Get all configs

// Check existence
has_config('key');

// Update
set_config_value('key', 'new value');  // Quick value update
update_config($config, $configItem);    // Full update

// Delete
delete_config($config);

// Nested
read_nested('mail');  // Get all configs under 'mail' namespace

return [
    'table' => env('LARAVEL_CONFIG_TABLE', 'laravel_config'),
];

// Before (v5.x)
use TarfinLabs\LaravelConfig\LaravelConfig;
$manager = new LaravelConfig();

// After (v6.x)
use TarfinLabs\LaravelConfig\ConfigManager;
$manager = new ConfigManager();

// Before (v5.x)
use TarfinLabs\LaravelConfig\LaravelConfigFacade;

// After (v6.x)
use TarfinLabs\LaravelConfig\Facades\LaravelConfig;
bash
php artisan vendor:publish --provider="TarfinLabs\LaravelConfig\LaravelConfigServiceProvider" --tag="laravel-config"
bash
php artisan migrate
bash
php artisan vendor:publish --provider="TarfinLabs\LaravelConfig\LaravelConfigServiceProvider" --tag="laravel-config"