Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jeetgeg's avatar

How to create Dynamically create config/custom.php config file

I am using a package which reads the configuration from config/custom.php config file. Whenever I add a new entry in database, i have to add entry to that config file manually. is there any way, I can create or modify config/custom.php config file dynamically whenever there is new entry added to database. Please help

// dummpy data from config/custom.php

0 likes
8 replies
jeetgeg's avatar
// dummpy data from config/custom.php

<?php
return [
    'data' => [
        '1' => [
            'id' => 'some_id',
            'key' => 'ajdlkjalsdfjasd',
            'key_2' => '890809809',
        ],
       '2' => [
            'id' => 'some_id33',
            'key' => 'ajdlkjalsdfjasd33',
            'key_2' => '890809809333',
           ]
           ],
    // Default key_2
    'key_2' => '22222222222222222',
    'track_log' => false
];
Cronix's avatar

You don't actually have to manipulate the files. You can add/alter config values at any time within the app.

echo config('app.name'); // echos the 'name' key set in config/app.php

Config::set('app.name', 'New Name');
echo config('app.name'); // New Name

You could create a middleware that reads from the db, and just sets the config options dynamically. Then they will always just be in-sync.

Of course another option is to create your own "SiteConfig" model class or something that reads from the db, and use those values instead of using config().

Another option is to hook into model events (created/updated/deleted/etc) and regenerate a custom config file automatically whenever you alter your config db table.

https://laravel.com/docs/5.6/eloquent#events

jeetgeg's avatar

Thanks Cronix,

I am new to laravel actually.

I want to create config file one time then cached. Whenever there is a new entry in Database. Config file should also be updated .

The package i am using is in vendor/somepackage .. That package is actually using the custom.php config file. I can not change the vendor/somepackage functionality.

I have tried to make a artisan command , and then try to make a change in custom.php config file using the Config:set() method but there is not update in custom.php file. It changes in runtime only. I don't want to query database at every request.

I just want to put all the configuration to custom.php file whenever there is any database entry modification or creation.

Config::set('app.name', 'New Name');

And there is no fix number of entries in database .

Cronix's avatar
Cronix
Best Answer
Level 67

Ok, I'd probably use model events (linked above) then so it can automatically regenerate the file whenever you change the data in the database table.

Make a query to get the values and build up your array how you'd need it.

$myarray = [
    'data' => [
        '1' => [
            'id' => 'some_id',
            'key' => 'ajdlkjalsdfjasd',
            'key_2' => '890809809',
        ],
       '2' => [
            'id' => 'some_id33',
            'key' => 'ajdlkjalsdfjasd33',
            'key_2' => '890809809333',
           ]
           ],
    'key_2' => '22222222222222222',
    'track_log' => false
];

// create the array as a php text string
$text = "<?php\n\nreturn " . var_export($myarray, true) . ";";

Then save $text as /config/custom.php

If you're caching the config file, you'd have to run an artisan command after saving the file.

Artisan::call('config:cache');

https://laravel.com/docs/5.6/artisan#programmatically-executing-commands

2 likes
spekkionu's avatar

You are probably better off using the cache component rather than the config component.

Cronix's avatar

@spekkionu How would that work if he's using a package that reads the values from config/custom.php?

Please or to participate in this conversation.