I have a database table of questions, answers, responses, and respondents. Questions have answers, which have responses, which are owned by a respondent (Respondents can have many responses).
I'm trying to make correlations by zeroing in on all the responses to questions related through respondents who chose a certain answer to a question. So, what else did people who answered that they were 18 - 20, answer.
The trick is I want to do this across multiple respondents and multiple answers. For example, what else did people who are 18 - 20, and like the color green like:
I currently have this working in an "or" fashion where each additional answer_id I add pulls in respondents who chose either one of the two answers:
$cFilters = array() of answer_ids;
$respondents = Auth::user()->account->respondents()
->whereHas('responses', function($q) use($cFilters) {
$q->whereIn('responses.answer_id', $cFilters);
})->get();
I'd ultimately like to have them working in an "and" fashion where adding in another answer_id shows only respondents who have answered both questions, can't quite figure that one out. Here is what I've tried so far that doesn't raise errors, but also doesn't seem to work correctly:
$cFilters = array() of answer_ids;
$respondents = Auth::user()->account->respondents()
->whereHas('responses', function($q) use($cFilters) {
foreach($cFilters as $answer_id) {
$q->where('responses.answer_id', $answer_id);
}
})->get();
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire