Why wouldn't result table contain both user_id and test_id? It would been easier then, right ?
Looking at best practice for how to construct and maintain many through many relationships
Good Afternoon,
I am a teacher constructing a site for tracking test results, but cannot get my head around the relationships.
I am envisioning a schema that has students these have many results and a test also has many results. Therefore students should have many tests through many results?
Additionally students will have many classes so there is a group_student pivot table.
How would I call principally all student results for a test where group = foobar?
Is there not an easy way to construct multiple nested relationships in laravel or is this unnecessary and I should just be retrieving each set in turn.
I have made a provisional "map" on laravelsd available:
http://www.laravelsd.com/share/zU8Xrs
Any help would be greatly appreciated,
Thanks!
if you pass parameter to whereHas() like I did above your not only querying existence but existence of relation with additional query that you've passed so i would get results like this:
Result::whereHas('students', function($query) use ($groupId) {
$query->join('group_student', 'group_student.student_id', '=', 'students.id')
->where('group_student.group_id', $groupId);
}):
I think that you can't use whereHas() in callback of whereHas(), so i used join for second relation....
Please or to participate in this conversation.