2016-07-28 58 views
0

我有一張名爲'templates'和'details'的表格,它具有關係模板,在我的模型中有很多細節。Laravel 5.2選擇相關計數數據的位置

我創建一個帶有過濾器編號或詳細信息的列表模板表,當我向過濾器框輸入5時,表中只顯示具有5個細節的模板。

該怎麼辦?

這是我的表結構:

模板

ID |名稱|寬度|身高

1 | A-5 | 112 | 100

2 | A-4 | 225 | 200個

細節

template_id | x | y

1 | 10 | 10

1 | 20 | 10

2 | 10 | 10

2 | 20 | 10

$templates = Template::whereHas('details', function($detail) { 
    $detail->selectRaw('count(id) as aduh')->whereRaw('count(id) = 100')->groupBy('id'); 
}); 
+0

Is * details *您的表格中有一列嗎?你可以添加你的表格結構和你期望得到的結果嗎? – TheFallen

回答

2

我認爲這應該工作:

$templates = Template::has('details', '=', 5)->get(); 

這將返回所有模板有5個細節

+1

謝謝你,我想找的這段代碼,我覺得太難了 –