Dynamic Eloquent Query
As is par for the course for me, I am drawing a blank (or maybe overthinking) something that is probably not too difficult to solve.
Setting the stage
I have a table that contains student assessment data. It looks something like this (excuse the mess; Laracasts never formats this properly for me, even after using Git-flavored code blocks):
id / 123
user_id / 28
room_id / 11
student_id / 62
subject_id / 23
unit_id / 203
outcome_id / 850
assessed_level / 4
created_at / 2018-02-21 21:08:50
updated_at / 2018-02-21 21:08:50
(repeated x however many assessments have been done)
Further explanation
Each teacher (user_id) can teach multiple rooms (room_id) and have any number of students (student_id) within those rooms. They can assess those students any number of times (assessed_level) for any of the subjects that they teach (subject_id). [The unit_id and outcome_id are not part of the differentiation in this particular view.]
What I want to do
What I want to do for one view is output the number of students (count) assessed at a 4 (assessed level) for each of the subjects (subject_id) over a period of time (created_at).
An example
For example, let's say a teacher has assessed their class of students 4 times in science. Each student earned a score (between 1 and 4) each of these 4 times. I want to add together and average out the score for each student in this subject.
Johnny = 4, 4, 3, 4 (average: 3.75, rounded up to 4)
Sally = 1, 2, 3, 4 (average: 2.5, rounded up to 3)
Johnny = 4, 4, 3, 3 (average: 3.5, rounded up to 4)
In this case, I would have 2 students who earned a 4 in science and 1 student who has earned a 3 in science for the given time period.
How would I write this programmatically? I have put all of the required data (I think) into one table so that the Eloquent call can be super straightforward, but I am scratching my head as to how to actually write it dynamically (scaling).
Please or to participate in this conversation.