Wakanda's avatar
Level 10

How to edit environment variables from browser or user dashboard?

Hi devs,

I am updating my Laravel e-commerce app and would want admin users to update the .env variables from their dashboard. How can I achieve this?

0 likes
1 reply
robsykes's avatar

Hi,

I'm not sure you can, well not without creating a script to do so. Even then, it's a bit risky.

Really, you shouldn't allow them to edit .env files directly, instead, provide a config and allow them to specify values there.

If it's crucial those changes must be done inside of the app via a form, then you can create a database table with fields for each aspect you wish to change and load those into the config at runtime.

That said - if you are eager to allow that functionality and let them edit the .env file, you could load the .env file into a textbox on an authenticated page, and when you save it, save it over .env file. Basic file input/output can handle this. Make sure the server user (nginx/apache/etc) has permissions to edit the file.

Let me know if you need me to clarify some more.

Please or to participate in this conversation.