2016-11-07 45 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