hoyvoy / laravel-cross-database-subqueries

Eloquent cross database compatibility in subqueries
MIT License
103 stars 45 forks source link

Issue when 1 database has a prefix #5

Closed samloft closed 5 years ago

samloft commented 6 years ago

Description:

When having 2 database, and one of those database has a table prefix, the table prefix is getting appended to the subquery on whereHas().

Steps To Reproduce:

Have 2 connection, e.g:

'1' => [
            'driver'   => 'sqlsrv',
            'host'     => env('DB_HOST', 'localhost'),
            'port'     => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset'  => 'utf8',
            'prefix'   =>'anything_,
        ],

'2' => [
            'driver'   => 'sqlsrv',
            'host'     => env('DB_HOST', 'localhost'),
            'port'     => env('DB_PORT', '1433'),
            'database' => env('DB_SAGE', 'demo'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset'  => 'utf8',
            'prefix'   => '',
        ],

Add relationships and two models, 1 for each database and an example function

return (new Orders)->whereDate('processed_at', '<=', Carbon::now()->subHours(1))->whereHas('orderBook', function($query) {
            $query->where('alpha', 'ZZZ');
        })->get();