Search code examples
laravelconfig

Laravel - use my custom helper functions in config files


I have this file config-cache.php in the config folder (config/config-cache.php), but when I use my helpers to get the values from the database, it doesn't work! what should i do Can I get these duration values from dynamic databases?

config-cache.php contents :

    'configs' => [
        'default' => [
            'prefix' => '_default_',
            'duration' => '+1 Year',
        ],
        'tiny' => [
            'prefix' => '_tiny_',
            'duration' => '+15 minutes',
        ],
        'short' => [
            'prefix' => '_short_',
            'duration' => '+1 hour',
        ],
        'otp' => [
            'prefix' => '_otp_',
            'duration' => '+2 minutes',
        ],
        'block' => [
            'prefix'   => '_block_',
            'duration' => '+5 minutes',
        ],
        'location' => [
            'prefix'   => '_location_',
            'duration' => '+600 minutes',
        ],
    ],
];

Solution

  • 1- Create a model to interact with your database table

    2- In your controller or service provider, retrieve the configuration values from the database and override the configuration:

    use Illuminate\Support\Facades\Config as LaravelConfig;
    
    // ...
    
    $configs = Config::all();
    
    foreach ($configs as $config) {
        LaravelConfig::set("config-cache.configs.{$config->name}", [
            'prefix'   => $config->prefix,
            'duration' => $config->duration,
        ]);
    }