2014-12-28 71 views
1

我有一個表結構與幾個多對多的聯接。訪問多個級別的許多許多兒童與雄辯

nodes -<node_template>- templates -<placeholder_template>- placeholder 

我有我的節點,模板和佔位符模型建立與belongsToMany方法主要表格是通過連接表的鏈接。

例如

public function byFileMakerIdWithTemplates($id){ 
    return $this->node->with('templates')->where('internal_id', '=', $id)->firstOrFail(); 
} 

有沒有使用口才選擇多個級別的子節點的方法?

也就是說,我可以查詢特定的node記錄並獲取template子記錄和模板的placeholder記錄嗎?喜歡的東西:

public function byFileMakerIdWithTemplates($id){ 
    return $this->node->with('templates')->with('placeholders')->where('internal_id', '=', $id)->firstOrFail(); 
} 

我知道我可以用DB門面給你寫的MySQL查詢我需要抓住所有使用JOIN條款或編寫代碼的數據來找到它的所有節點的模板和然後通過每個模板查找佔位符,但是如果有辦法在多維數組中完全抓住它,那將是太棒了。

回答

0

你可以貪婪加載深度嵌套關係通過.點語法:

with('relationA.relationB'); 

所以你可以這樣寫:

public function byFileMakerIdWithTemplates($id){ 
    return $this->node->with('templates.placeholders')->where('internal_id', '=', $id)->firstOrFail(); 
} 
+0

完全,做的工作。感謝您的正確方向! –

+0

嗨,先生,我只需要一個小幫助,http://chat.stackoverflow.com/rooms/67732/discussion-between-biz-dev-b-and-lukasgeiter我將立即刪除此評論 –

+1

@lukasgeiter你不'在這種情況下,需要分別指定每個嵌套級別,因此只需使用('relation.nested.anotherLevel')'。 –