我有一个具有多到多个连接的表结构。
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子句获取所有数据,或者编写代码来查找节点及其所有模板,然后预先遍历每个模板以找到占位符,但是如果有一种方法可以在多维数组中干净地抓取这些数据,那就太棒了。
发布于 2014-12-29 00:04:56
您可以使用.点语法来加载深度嵌套的关系:
with('relationA.relationB');所以你会写:
public function byFileMakerIdWithTemplates($id){
return $this->node->with('templates.placeholders')->where('internal_id', '=', $id)->firstOrFail();
}https://stackoverflow.com/questions/27681808
复制相似问题