laravel - Is it possible to have a HasManyThrough relationship that's deeper than two levels? -
i have following models:
product baseproduct basecode series
each of them of separate entity, they're related.
product
has foreign key baseproduct
, baseproduct
has foreign key basecode
, basecode
has foreign key series
.
i have specified method within basecode
model can select of products
related particular basecode
:
public function products() { return $this->hasmanythrough('product', 'baseproduct', 'basecodeid', 'baseproductid'); }
basecodeid
pk basecode
, fk in baseproduct
pointing pk, , baseproductid
pk baseproduct
, , in product
table, there's fk points it.
this , good, , works dandy me.
i'd go 1 level further though, , define following in series
model:
public function products() { //it's here i'd have no idea - there three-level deep "hasmanythroughthrough" or something? return $this->baseproducts()-> /* , whatever i'd here products */ } public function baseproducts() { return $this->hasmanythrough('baseproduct', 'basecode', 'seriesid', 'basecodeid'); }
how of product
s associated series
that?
i new 4.1, looks though hasmanythrough()
not designed types of relationships looking , shortcut 2 level deep eager loading.
try traditional eager loading instead...
$data = product::with('baseproduct.basecode.series');
you need make sure relationships setup in models necessary , instead of calling model names, want call methods defining relationships.
also obviously, sure models know primary keys using protected $primarykey = 'seriesid'
etc...
Comments
Post a Comment