2017-09-02 79 views
0

我有一些表:渴望負載嵌套的倍數關係

  • 記錄
  • record_relations
  • 標識
  • 庫存
  • 價格

Record具有多態性關係一起涉及IdentificationInventoryPriceInventoryMetricPrice有關係,與Currency有關係。所以......

  • RecordhasManyRecordRelations通過relations()
  • RecordRelationmorphToIdentificationInventoryPrice通過relation()
  • InventorybelongsToMetric通過metric()
  • PricebelongsToCurrency通過currency()

如果我懶加載一切正常,但我需要加載所有關係(我將它們顯示在數據表中);嘗試了很多之後,我能夠加載最深的關係:如果我添加其他的關係我已經清空所有關係

$builder->with([ 
    'relations' => function($query) { 

     $query->with('relation.metric')->where('relation_type', '=', 'App\\Inventory'); 

    } 
]); 

但是......

$builder->with([ 
    'relations' => function($query) { 

     $query->with('relation'); 
     $query->with('relation.metric')->where('relation_type', '=', 'App\\Inventory'); 
     $query->with('relation.currency')->where('relation_type', '=', 'App\\Price'); 

    } 
]); 

看來我不能,某些原因,「withWhere」不止一次。有沒有辦法做到這一點?

回答

0

定義關係如波紋管

$builder->with([ 
    'relations' => function($query) { 
     $query->addSelect('column_names'); 
    }, 
    'relation.metric' => function($query){ 
     $query->addSelect('column_names')->where('_condition goes here'); 
    }, 
    'relation.currency' => function($query){ 
     $query->addSelect('column_names')->where('_condition goes here'); 
    } 

]);

將column_names替換爲要檢索的列的名稱。 和_condition與您想要應用的條件。

+0

爲了訪問'關係'我需要通過'relations.relation',這樣工作嗎? – fred00

+0

沒有它沒有必要...但如果關係表是從關係表中獲取引用,那麼它是必需的。 –