Fendi Tri Cahyono
Fendi Tri Cahyono

Reputation: 702

Database Connection From Session in Laravel

I have multiple database connection, but for perusahaan db connection I want set database_name from session. how to resolve it?

. enter image description here

Upvotes: 1

Views: 1664

Answers (2)

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28541

You need to do that at runtime, it cannot be done directly from the configuration file.

config(['database.connections.perusahaan.database' => session('db_key')]);

You may also need to purge the connection as it may be cached

app(DatabaseManager::class)->purge('perusahaan');

Upvotes: 0

online Thomas
online Thomas

Reputation: 9381

You could do this:

config(['database.connections.perusahaan.database' => session('db_key')]);

You set a config variable runtime with the config helper by passing a value to it from the session helper. The way you do it fails, because session is not yet available while reading config.

I would like to add a warning that you might trust data that should not be trusted, however I do not know enough about your app to be certain.

Upvotes: 2

Related Questions