hoyvoy / laravel-cross-database-subqueries

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

How can i use Laravel Cross database subqueries in different databases? #18

Open jlopez0313 opened 4 years ago

jlopez0313 commented 4 years ago

Is that possible to use Laravel CDS in two different schemas?

I have 2 connections in .env and database.php, but in whereHas it only use the main DB.. p.e. Users is located in schema 1 from DB A Posts is located in schema 2 from DB B

I have set the connection in both models and this works fine:

$query = \App\Users->with('Posts')->get();

But this code doesn't work fine: $query = \App\Users->with('Posts')->whereHas('Posts', function($q) { $q->where('post_type', '3');})->get();

Description:

Steps To Reproduce:

MwSpaceLLC commented 4 years ago

Hi there, we want to write that for us all work fine in mysql with package cross-database.

image

In this query User are in DB1 and categories are in DB2. the result work great!

For us, we must specify relation connection ('database.relation') in :

image

And for User, we must replace Authenticatable with new model class:

image

image

Thanks for the trick :+1:

Healyhatman commented 4 years ago

Having trouble with mine on SQL Server, the generated query looks like this

(SQL: select top 1 from [Clients] where exists (select from [TMACentral-Demo].[Cli_Core] where [Clients].[CLIENTNUM] = [Cli_Core].[SYSID])

When it should be [TMACentral-Demo].[dbo].[Cli_Core]