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

cconey's avatar

hasManyThrough with multiple connections

So I have a model that is using a table that is in a different database, and as such, uses a different connection. I have used the Eloquent property $connection in each model that is being loaded and the relationships work fine on their own. As soon as I introduce the whereHas on the hasManyThrough relationship, it fails to take the connections into consideration.

Is there anything else that needs to be done for connections to function properly when using a hasManyThrough relationship?

EDIT:

Here is the model with the relationship, not sure if that would help at all.

Click.php


<?php class Click extends Base { protected $primaryKey = 'click_id'; protected $connection = 'other'; protected $table = 'clicks'; public function programs() { return $this->hasManyThrough('Program', 'Product'); } }
0 likes
1 reply
Valorin's avatar
Valorin
Best Answer
Level 4

I'm pretty sure that the relationships only work across a single connection, since it uses SQL joins to retrieve the information. If the data is across separate connections, then an SQL join is not possible.

I'd recommend writing a special multi-connection relationship helper, and use that in place of the standard relationship helpers. Once the helper is written, your code can treat the models and relationships as per normal, but internally it does the magic required to load from both.

Please or to participate in this conversation.