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

lara144533's avatar

Database password receive from API request

I wanted to use DB_PASSWORD= from api request using Azure key Vault and store in session. so database password will not store in our web server at all. can you share me idea how i can implement this idea in Laravel-5.8

0 likes
4 replies
JohnBraun's avatar

You could override the config() value where DB_PASSWORD is stored at runtime, using:

config(['database.connections.mysql.password' => $passwordFromApi]);

However, I don't know which database driver you're using.

Do note that you can't override .env variables at runtime.

lara144533's avatar

I am using mysql driver. can you share me idea how i can call api before loading laravel application. and where i should declare this config i mean middleware/provider any file name.

I wanted to store session config such a way if someone hacked our webserver still they will not have database password

JohnBraun's avatar

Actually I don't think it is a good idea to store your password somewhere else than in your .env file.

Is there any particular reason why you want to do this?

lara144533's avatar

Our reason is that if our server is compromised/hacked then they will not have any data, they can not access our database.

if i use config(['database.connections.mysql.password' => $passwordFromApi]) then it will affect for all users. becouse its changes application level.

i wanted for single users when they request for login, receive db password from api and when they logout it will change with default user and password

Please or to participate in this conversation.