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

DanielLaravel's avatar

Database connections

I'm using Laravel 5. I see Laravel uses .env to connect databases for example DB_USERNAME and DB_PASSWORD, but i want to log in and use my username and password from my form to connect with those values from withim my oracle database with a table name "dba_users" which has the user and password and some others fields

i want to do that becauses each user has their own granted permissions(roles) to some tables, thus Oracle would manage the user login permissions and not the .env DB_USERNAME.

Maybe i could set a new connection parameters where i put my username and password sent by the login form?

Thank you Laravel community.

0 likes
3 replies
jbloomstrom's avatar

Typically, Laravel uses a "global" database connection and assumes you are handling access control and permissions at the application level. It's possible to use different connections, but I don't think it will be very manageable to manage roles this way in Laravel.

Instead, I would look at implementing access control or roles within the Laravel application. This package from Spatie looks like a good starting point.

1 like
Cronix's avatar
Cronix
Best Answer
Level 67

Create a new database config in /config/databases.php to use as the 2nd connection. You can leave some values blank, like username, password, database, etc.

'yourCustomConfig' => [
    'host' => '196.168.1.2'
    'driver'    => 'mysql',
    'database'  => '',
    'username'  => '',
    'password'  => '',
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix'    => '',
],

Then before you want to access it, you can change the values dynamically

Config::set("database.connections.yourCustomConfig", [
    "host" => "1.2.3.4",
    "database" => "differentDBName",
    "username" => "Billy",
    "password" => "Bob"
]);

Now any model using the 'yourCustomConfig' connection will use those values to connect for the duration of that request. You'd have to set it each time in between requests.

SomeModel::on('yourCustomConfig')->find(123);

or

DB::connection('yourCustomConfig')->table('someTable')->where('something', 'other')->get();

2 likes
jekinney's avatar

Set up another set of env variables, add another connection similar to the MySQL array but obviously name something else. In your user model you can set the connection to your oracle db:

protected $connection = 'name'

A caviate is you have to authorize users your self as the hash won't work (auth()->attempt()). But once you authorize the user you can manually log them in. auth()->login(userObject).

Unfortunately I'm in a similar boat using active directory (ms sql) for users and acl. Not a huge deal but a little extra work.

Please or to participate in this conversation.