Skip to main content

Overview

The Enrollment model manages the relationship between students and courses, handling enrollment operations and queries. File: app/models/Enrollment.php

Methods

getAll

Retrieves all enrollments with student and course information. SQL Query:
SELECT 
    students.id AS student_id,
    users.name AS student_name,
    subjects.name AS subject_name,
    courses.id AS course_id
FROM enrollments
JOIN students ON enrollments.student_id = students.id
JOIN users ON students.id = users.id
JOIN courses ON enrollments.course_id = courses.id
JOIN subjects ON courses.subject_id = subjects.id
result
array
Returns array of all enrollments with related data
Example:
$enrollment = new Enrollment();
$allEnrollments = $enrollment->getAll();

create

Creates a new enrollment for a student in a course.
student_id
int
required
ID of the student to enroll
course_id
int
required
ID of the course to enroll in
SQL Query:
INSERT INTO enrollments (student_id, course_id) VALUES (?, ?)
result
boolean
Returns true if enrollment was successful, false otherwise
Example:
$enrollment = new Enrollment();
$success = $enrollment->create(15, 8); // Student 15 enrolls in Course 8
Use isAlreadyEnrolled() before creating to prevent duplicate enrollments.

delete

Deletes an enrollment for a specific student and course.
student_id
int
required
Student ID
course_id
int
required
Course ID
SQL Query:
DELETE FROM enrollments WHERE student_id=? AND course_id=?
result
void
No return value (throws exception on failure)
Example:
$enrollment = new Enrollment();
$enrollment->delete(15, 8); // Remove Student 15 from Course 8

getByStudent

Retrieves all enrollments for a specific student.
student_id
int
required
Student ID to filter by
SQL Query:
SELECT enrollments.*
FROM enrollments
WHERE enrollments.student_id = ?
result
array
Returns array of enrollment records for the student
Example:
$enrollment = new Enrollment();
$studentEnrollments = $enrollment->getByStudent(15);

isAlreadyEnrolled

Checks if a student is already enrolled in a specific course.
student_id
int
required
Student ID to check
course_id
int
required
Course ID to check
SQL Query:
SELECT COUNT(*) FROM enrollments WHERE student_id = ? AND course_id = ?
result
boolean
Returns true if student is already enrolled, false otherwise
Example:
$enrollment = new Enrollment();
if (!$enrollment->isAlreadyEnrolled(15, 8)) {
    $enrollment->create(15, 8);
}
This method is useful for preventing duplicate enrollments before calling create().

Build docs developers (and LLMs) love