2016-11-07 31 views
0

我有以下关系:Laravel雄辩 - 方法来获得“孙子”,其中grandparent_id匹配

Tool: 
----- 
id 
name 
description 


Competency: 
----------- 
id 
name 
description 
tool_id 


Indicator: 
---------- 
id 
name 
description 
competency_id 

我想获得的所有直接落在指定工具下的指标(即孙子) 。关系是工具有很多能力,能力有很多指标。

我()到目前为止的代码是:

hasManyThrough('Indicator', 'Competency', 'tool_id', 'competency_id')->where('tool_id', '=', 1)->get(); 

我想我要下去右线,但遇到麻烦的代码的权利。

+0

我想你想告诉 “*工具有很多能力和竞争力有很多指标** **(而不是工具)*” 没有? – tomloprod

+0

是的,我的错误。我会更新这个 – ugotchi

回答

1

获取指示能力的指标。

$indicatorsOfCompetency = Indicator::where("competendy_id", $yourCompetencyID)->get(); 

,或者取决于你laravel关系的名字的:

$competency = Competency::find($yourCompetencyID); 

$indicatorsOfCompetency = $competency->indicators()->select(["id","field1","field2"]); 


获取指标时,他的能力它关系到指定的工具。

$competencyIDS = Competency::where("tool_id", $yourToolID)->lists("id"); 

$indicators = Indicator::whereIn("competency_id", $competencyIDS); 

以上雄辩

$indicators = Indicator::whereHas("competency", function($q) use ($yourToolID){ 
    $q->whereHas("tools", function($q2) use($yourToolID){ 
     $q2->where("id", $yourToolID); 
    }); 
}); 
+0

我们正在尝试传递tool_id和过滤器,以便仅查看直接位于下方的指标,而不考虑能力。由于我们没有引用任何competency_id,因此上面的内容如何让我们这样做? – ugotchi