Skip to main content

Overview

The Student model manages student-specific operations including creation, retrieval, deletion, and search. File: app/models/Student.php

Methods

getAll

Retrieves all students with their user information. SQL Query:
SELECT users.id, users.name, users.email 
FROM users 
JOIN students ON users.id = students.id
ORDER BY users.name
result
array
Returns array of all student records ordered by name
Example:
$student = new Student();
$allStudents = $student->getAll();

create

Creates a new student record in both users and students tables.
name
string
required
Student’s full name
email
string
required
Student’s email address
password
string
required
Plain text password (will be hashed)
SQL Queries:
-- 1. Insert into users table
INSERT INTO users (name, email, password, role) VALUES (?, ?, ?, 'student')

-- 2. Insert into students table
INSERT INTO students (id) VALUES (?)
result
boolean
Returns true if student creation was successful, false otherwise
Example:
$student = new Student();
$success = $student->create('Jane Doe', '[email protected]', 'password123');
The password is automatically hashed using PHP’s password_hash() with PASSWORD_DEFAULT algorithm.

delete

Deletes a student record (cascades to students table via foreign key).
id
int
required
Student ID to delete
SQL Query:
DELETE FROM users WHERE id = ?
result
boolean
Returns true if deletion was successful, false otherwise
Example:
$student = new Student();
$success = $student->delete(5);
This operation deletes the user from the users table. The students table record is deleted automatically via CASCADE.

Searches for students by name or email using keyword matching.
keyword
string
required
Search keyword (supports partial matches)
SQL Query:
SELECT students.*, users.name, users.email
FROM students
JOIN users ON students.id = users.id
WHERE users.name LIKE ? OR users.email LIKE ?
result
array
Returns array of matching student records
Example:
$student = new Student();
$results = $student->search('john');
The search uses LIKE with wildcards (%keyword%) to match partial strings in both name and email fields.

Build docs developers (and LLMs) love