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

shipu's avatar

How to modify Config folder File

How to modify Config folder File. Suppose i have "custom.php" file in config folder. In "custom.php" file have below lines of code :

<?php
return [
    'disable'=>array(
    ),
];

how to modify this disable object that means how to push data in disable object using route or controller.

final output like below code :

<?php
return [
    'disable'=>array(
        teacher,
        student
    ),
];
0 likes
11 replies
ohffs's avatar

You really don't want to be altering config files from your http controller. If you need dynamic configuration you will be better with a database table.

shipu's avatar

Actually it's not possible to describe details why i need this. i don't want to use database table. Can you give me some solution how to modify Config folder database.php file. @ohffs

ohffs's avatar

You'd have to make the files/folder writeable by your webserver (like you do for the storage/ directory) then use things like fopen()/fwrite() etc - but this is a really, really bad idea and very prone to errors and problems. I can't emphasise enough what a bad idea this is.

michaeldyrynda's avatar

You're trying to set configuration on the fly?

You can use the Config facade to achieve this:

Config::set('custom.disable', [ 'teacher', 'student', ]);

Which will apply for the request, but as @ohffs suggests above, I wouldn't do it. If a value is changing at runtime, it probably doesn't belong in a static configuration value.

1 like
ohffs's avatar

Can I just re-iterate that this is a really bad idea? :-)

1 like
michaeldyrynda's avatar

Why wouldn't you just set them in the config file itself, then? Each time you need to change the values, update the config file, then push and redeploy the config. Or, as @ohffs said, shift the (I presume) user-modifiable values to the database.

acacha's avatar

@ohffs @deringer let me expose and example I think it could be useful to "dinamically/programatically" modify config file:

Command line execution, something like artisan custom commands.

For example I'm tired every time I've to install a new laravel service provider from a package, everytime we do the same adding a provider to providers array i config/app file. Imagine how could would be:

 $ php artisan provider:add "\App\MyProvider"

So anyone now have a recommendation of hot to easily modify Laravel config files programatically?

ohffs's avatar

@acacha doing it from the commandline is a different prospect though, surely? If you wanted you could just rm -fr all the config files. Which isn't something you want to happen via your website, usually ;-) Oddly enough, for your specific example there was a tool mentioned recently to speed up adding providers : https://github.com/remoblaser/lazy-artisan

2 likes
acacha's avatar

Lazy artisan it seems to me a really good tool/idea and I think something similar would be very useful at Laravel by default!

1 like

Please or to participate in this conversation.