Eager loading from different connection does not seem to work - Laravel

Viewed 24

I have a project that uses an external MSSQL database and a local MySQL database.

I use the local mysql connection to store in images that i retrieve from the MSSQL database. This works all fine, but when i try to eager load the local images it produces no query at all and returns an empty collection object.

In my product model i have these 2 relations:

public function images()
{
    return $this->belongsToMany(Afbeelding::class, 'VW_Web_Artikel_Afbeeldingen', 'Artikel_ID', 'Afbeelding_ID')->withPivot('Standaard');
}

public function productImages()
{        
    return $this->setConnection('mysql')->hasMany(ProductImage::class, 'product_id', 'id');
}

The first on is for the MSSQL database, the 2nd one is the MYSQL database.

Now when i eager load the images from the MSSQL database, everything works as expected:

    DB::enableQueryLog();
    $a = Artikel::where('id', $id)->with('images')->first();

    dd(DB::getQueryLog());

Produces the following queries:

"select top 1 * from [VW_Web_Data_Artikelen] where [id] = ?"
select [VW_Web_Afbeeldingen].*, [VW_Web_Artikel_Afbeeldingen].[Artikel_ID] as [pivot_Artikel_ID], [VW_Web_Artikel_Afbeeldingen].[Afbeelding_ID] as [pivot_Afbeelding_ID], [VW_Web_Artikel_Afbeeldingen].[Standaard] as [pivot_Standaard] from [VW_Web_Afbeeldingen] inner join [VW_Web_Artikel_Afbeeldingen] on [VW_Web_Afbeeldingen].[ID] = [VW_Web_Artikel_Afbeeldingen].[Afbeelding_ID] where [VW_Web_Artikel_Afbeeldingen].[Artikel_ID] in (?)

Now when i try to eager load from the local mysql connection, no query is done at all:

    DB::enableQueryLog();
    $a = Artikel::where('id', $id)->with('productImages')->first();

    dd(DB::getQueryLog());

Produces 1 query:

select top 1 * from [VW_Web_Data_Artikelen] where [id] = ?

So the image query is skipped entirely. I can work around this, but i'd like to understand why this is not possible. Could someone enlighten me?

0 Answers
Related