Other types of relationships work with multiple database connections:
public function foos() { return $this->belongsToMany(Foo::class, 'other_db.foos'); }
But hasManyThrough does not have the $table parameter in its signature, so the same solution is not applicable.
but
You can make the wrong workaround as follows:
public function bars() { return $this->belongsToMany(Bar::class, 'other_db.bars'); } public function foos() { $barIds = $this->bars->pluck('id'); return Foo::whereIn('bar_id', $barIds); }
It does not offer exactly the same functionality (since this is a different type of return), but fulfills the purpose for simpler things.
If you want, you can also reproduce another syntax by doing something like this:
protected $appends = [ 'foos', ]; public function getFoosAttribute() { return $this->foos()->get(); }
That way, you can still use it in your code, as in most cases, with regular relationships (which means you can use $this->foos instead of $this->foos()->get() )
OskarD90
source share