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

chgoub's avatar
Level 1

Loss of session messages following artisan optimize and redirection

Hi all,

My project is run on Laravel 10

For some reason I need to be able to update .env's data and the corresponding config's file. I managed to do so in the controller's store method and then I called

Artisan::call("optimize");

before redirecting to the page I came from

return back()->with("success", $message);

I can see the session data if I log them before the redirection but on the page I am redirected to I don't have them any more

Furthermore If I comment the artisan call, I can see the session data displayed on the page I have been redirected but my new config is not taken into account.

How can I do then ?

Thanks in advance for your answers

0 likes
7 replies
tykus's avatar

You can cache the config directly which should be enough to take account of the .env changes.

Artisan::call('config:cache')

Aside, I don't understand what you are doing or why? How are you changing the .env file if not also able to execute the artisan command directly?

Tray2's avatar

Like @tykus says you should be able to run any artisan command programmatically, but why are you changing the env? It sounds to me that you should move those things into the database instead.

Snapey's avatar

For some reason I need to be able to update .env's data

For no good reason.

This is and will always be a horrid hack

chgoub's avatar
Level 1

@Snapey @tray2 @tykus Thanks all for your answers. I know it's not a good idea but the client does not want to bother with writing things into the .env file and wants to be able to update the configuration whenever he wants through a Laravel interface.

tykus's avatar

@chgoub this will not end well...

wants to be able to update the configuration whenever he wants through a Laravel interface

1 like
Snapey's avatar

@chgoub programmatically edit the .env and stay away from caching would be my suggestion

be aware that a mistake in editing the env could lead to an inaccessible application, where you cannot recover without going into the server.

Please or to participate in this conversation.