I am trying to use the hasManyThrough relationship in laravel 5.1 but the sql query is not using the appropriate prefix defined in each connection for each model used. I have 3 models 2 of which use the same connection and one of them uses a different one. The only difference between the connections is the prefix the database is the same.
The relationship:
Inside Model B:
public function relationshipWithA()
{
return $this->hasManyThrough(A::class, C::class, 'Cid', 'Aid');
}
The final query logic is correct but instead of using the B_ prefix for the joined tables its using the A_ prefix for all the tables in the query.
Is this a bug/limitation of laravel? Is there a solution to this or I will have to do manual join to achieve what I want?
Other relationship types 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.
However,
You can make an imperfect workaround like this:
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 the exact same functionality (Since it is a different return-type), but fulfills the purpose for more simple things.
If you want, you can also replicate some more of the syntax by doing something like this:
protected $appends = [
'foos',
];
/**
* @return Foo[]
*/
public function getFoosAttribute()
{
return $this->foos()->get();
}
That way you can still use it in your code like you would most of the time with a regular relationship (Meaning you can use $this->foos
instead of $this->foos()->get()
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With