yajra / laravel-oci8

Oracle DB driver for Laravel via OCI8
https://yajrabox.com/docs/laravel-oci8
MIT License
834 stars 237 forks source link

How i pass user and password for every connection? #320

Closed kindaniel closed 7 years ago

kindaniel commented 7 years ago

Summary of problem or feature request

How i make an alternative connection, passing user and password for every connection?

Code snippet of problem

I tryed to make that, but it not works..

{ public function login(Request $request){

    $username = "test";
        $password = 123;
        $select = "SELECT * FROM SYSDBA_USERS"
    $conexaoOracle = $this->oracleConnect($username, $password);
    //dd($conexaoOracle);
    $conn = DB::connection('oracle');    
    $conn->config = $conexaoOracle;    
    $result = DB::connection($conn)->select($select);
    return json_encode($result);   
}

public function oracleConnect($user, $password){
    return ['driver'        => 'oracle',
        'tns'           => 'SNT01TST',
        'host'          => 'host',
        'port'          => '1521',
        'database'      => 'database',
        'username'      => $user,
        'password'      => $password,
        'charset'       => env('DB_CHARSET', 'AL32UTF8'),
        'prefix'        => env('DB_PREFIX', ''),
        'prefix_schema' => env('DB_SCHEMA_PREFIX', ''),        
    ];

}

System details

mstaack commented 7 years ago

you need to specifiy each connection under config/database.php

like here:


    'connections' => [
        'first' => [
            'driver'        => 'oracle',
            'tns'           => env('DATABASE_TNSNAME', 'RACNAME'),
            'service_name'  => env('DB_SERVICE_NAME', 'SERVICENAME'),
            'host'          => env('DB_HOST', ''),
            'port'          => env('DB_PORT', '1521'),
            'username'      => env('DATABASE_USER', ''),
            'password'      => env('DATABASE_PASSWORD', ''),
            'database'      => env('DB_DATABASE', ''),
            'charset'       => env('DB_CHARSET', 'AL32UTF8'),
            'prefix'        => env('DB_PREFIX', ''),
            'prefix_schema' => env('DB_SCHEMA_PREFIX', ''),
            'options'       => [
                PDO::ATTR_PERSISTENT => true
            ],
        ],
        'second' => [
            'driver'        => 'oracle',
            'tns'           => env('DATABASE_TNSNAME', 'RACNAME'),
            'service_name'  => env('DB_SERVICE_NAME', 'SERVICENAME'),
            'host'          => env('DB_HOST', ''),
            'port'          => env('DB_PORT', '1521'),
            'username'      => env('DATABASE_USER', ''),
            'password'      => env('DATABASE_PASSWORD', ''),
            'database'      => env('DB_DATABASE', ''),
            'charset'       => env('DB_CHARSET', 'AL32UTF8'),
            'prefix'        => env('DB_PREFIX', ''),
            'prefix_schema' => env('DB_SCHEMA_PREFIX', ''),
            'options'       => [
                PDO::ATTR_PERSISTENT => true
            ],
        ],

then later in your code you can use: DB::connection('first')->select ....

kindaniel commented 7 years ago

Thank you for you answer. But i want this users and password dynamically...

mstaack commented 7 years ago

have a look online this is not specific to laravel-oci8!

https://lukevers.com/2015/03/25/on-the-fly-database-connections-with-laravel-5 https://laracasts.com/discuss/channels/tips/set-up-dynamic-database-connection-globally

MehdiAroui commented 3 years ago

Hi Kindanial,

try to change the configs like that :

config([
    'database.connections.oracle.host' => $host,
    'database.connections.oracle.port' => $port,
    'database.connections.oracle.username' => $username,
    'database.connections.oracle.password' => $password,
]);

\DB::purge('oracle');

because, the package merge the config from oracle.php to databases.php file. and use them from there.

Good luck.