php - Different db connection for models using hasManyThrough relationship in laravel 5.1 -


i trying use hasmanythrough relationship in laravel 5.1 sql query not using appropriate prefix defined in each connection each model used. have 3 models 2 of use same connection , 1 of them uses different one. difference between connections prefix database same.

  • model has connection uses prefix a_
  • model b has connection b uses prefix b_
  • model c has connection b uses prefix b_

the relationship:

inside model b:

public function relationshipwitha() {     return $this->hasmanythrough(a::class, c::class, 'cid', 'aid'); } 

the final query logic correct instead of using b_ prefix joined tables using a_ prefix tables in query.

is bug/limitation of laravel? there solution or have manual join achieve want?

other relationship types work multiple database connections:

public function foos() {     return $this->belongstomany(foo::class, 'other_db.foos'); } 

but hasmanythrough not have $table parameter in signature, same solution not applicable.

however,

you can make imperfect workaround 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 not offer exact same functionality (since different return-type), fulfills purpose more simple things.

if want, can replicate more of syntax doing this:

protected $appends = [     'foos', ];  /**  * @return foo[]  */ public function getfoosattribute() {     return $this->foos()->get(); } 

that way can still use in code of time regular relationship (meaning can use $this->foos instead of $this->foos()->get())


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -