0
我得到了一個調查表的設置,每個調查都有一些與自身相關的答案(一對多)。我想做一個查詢來計算每個surveyId的答案數分別爲parameter1 = 0和parameter1 = 1。合併兩個Eloquent集合
我試着這樣做:
$surveysWithRepliesExposed = Survey::where('title', 'LIKE', "%$name%")
->with(array('surveyAnswerSets' => function($query){
$query->where('parameter1', 0)
->selectRaw('surveyId, count(*) as repliesExposed')
->groupBy('surveyId');
}))
->select($columns);
$surveysWithRepliesExposed = Survey::where('title', 'LIKE', "%$name%")
->with(array('surveyAnswerSets' => function($query){
$query->where('parameter1', 1)
->selectRaw('surveyId, count(*) as repliesControl')
->groupBy('surveyId');
}))
->select($columns);
但現在我得到的外觀幾乎相同的兩個集合:
{
"surveyId": 11,
"title": "A survey",
"startDate": "2015-04-16",
"endDate": "2015-04-30",
"survey_answer_sets": [
{
"surveyId": 11,
"repliesExposed": 212
}
]
}
而其他
{
"surveyId": 11,
"title": "A survey",
"startDate": "2015-04-16",
"endDate": "2015-04-30",
"survey_answer_sets": [
{
"surveyId": 11,
"repliesControl": 56
}
]
}
如何合併這些讓我得到
{
"surveyId": 11,
"title": "A survey",
"startDate": "2015-04-16",
"endDate": "2015-04-30",
"survey_answer_sets": [
{
"surveyId": 11,
"repliesExposed": 212
"repliesControl": 56
}
]
}