Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public function index()
{
//
$scores = \DB::table('scores')
->leftjoin('students', 'students.id', '=', 'scores.student_id')
->leftjoin('subjects', 'subjects.id', '=', 'scores.subject_id')
->leftjoin('grades', 'grades.id', '=', 'scores.grade_id')
->leftjoin('terms', 'terms.id', '=', 'scores.term_id')
->select('students.first_name', 'students.surname', 'subjects.name as subject', 'grades.name as grade', 'terms.name as term', 'score')
->where('students.id', 1)
->whereBetween('scores.term_id', [1, 3])
->get();
$scores_paired = collect();
$subjects = Subject::all();
foreach($subjects as $subject){
$scores_paired->push($scores->where('subject', $subject->name)->sortBy('term'));
}
// finding details of the student based on id pased
//$student = Student::findOrFail(1);
// getting the name of the student
//$name = $student->first_name.' '.$student->surname;
// getting the class or grade of the student (grade 12 or grade 11)
//$grade = $student->grade->name;
// getting the current date
//$date = Score::date();
//return view('scores.home', compact('scores', 'name', 'date', 'grade'));
dd($scores_paired);
}
results from what I have.
Collection {#194 ▼
#items: array:6 [▼
0 => Collection {#224 ▼
#items: array:1 [▼
0 => {#215 ▼
+"first_name": "Nathan"
+"surname": "Siafa"
+"subject": "Mathematics"
+"grade": "12th Grade"
+"term": "1st Period"
+"score": 99.0
}
]
}
1 => Collection {#223 ▼
#items: []
}
2 => Collection {#222 ▼
#items: array:2 [▼
2 => {#211 ▼
+"first_name": "Nathan"
+"surname": "Siafa"
+"subject": "Biology"
+"grade": "12th Grade"
+"term": "2nd Period"
+"score": 99.0
}
1 => {#216 ▼
+"first_name": "Nathan"
+"surname": "Siafa"
+"subject": "Biology"
+"grade": "12th Grade"
+"term": "3rd Period"
+"score": 79.0
}
]
}
3 => Collection {#221 ▼
#items: array:1 [▼
3 => {#214 ▼
+"first_name": "Nathan"
+"surname": "Siafa"
+"subject": "Physics"
+"grade": "12th Grade"
+"term": "1st Period"
+"score": 70.0
}
]
}
4 => Collection {#218 ▼
#items: []
}
5 => Collection {#220 ▼
#items: []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment