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

Mahmoud04's avatar

Can update environment file value?

i use Spatie Setting

composer require spatie/laravel-settings

ineed update .env value

example: when update (app_name) update APP_NAME from .env file

i try this :

Config::set('app.name', $data['app_name']);

not work when try: config('app.name'); return old value

any idea!

0 likes
2 replies
jdc1898's avatar
Config::set('services.mailgun.username', $data['app_name']);

I believe doing it this way limits your change to only being in the current request as it will not persist beyond the execution of the code it's running in.

If you want it to persist, you will need it to update in the .env file.

file_put_contents(app()->environmentFilePath(), str_replace(
    'MAILGUN_USERNAME='.env('MAILGUN_USERNAME'),
    'MAILGUN_USERNAME=updatedUser',
    file_get_contents(app()->environmentFilePath())
));

then you need to clear the config.

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

If you do this often, it may be a good idea to build a helper method to do all of this in one step.

Please or to participate in this conversation.