Course Management Module
This is a Laravel API I built to manage courses, categories, students, and enrollments. It uses the Repository Pattern, optimized queries, and includes a Postman collection for testing.
What You Need PHP 8.1 or higher Composer MySQL Laravel 12.x
How to Set It Up
- Clone the Repository:
git clone <repository-url> cd course-management
- Install Dependencies:
composer install
- Configure Environment:
- Copy
.env.exampleto.env:cp .env.example .env
- Update
.envwith your database credentials.
- Copy
- Generate Application Key:
php artisan key:generate
- Run Migrations:
php artisan migrate
- Seed Database:
php artisan db:seed
- Serve Application:
php artisan serve
- Run Tests (optional):
php artisan test - API Testing:
- Import
CM.postman_collection.jsoninto Postman. - Set
baseUrltohttp://127.0.0.1:8000. - Run the following requests:
GET /api/courses: Lists all courses with category name and enrollment count.GET /api/courses/{courseId}/students: Lists students enrolled in the specified course.GET /api/courses/filter?category_id={id}&search={term}: Filters courses by category and/or name.
- Import
API Endpoints
GET /api/courses: List all courses with category and enrollment count.GET /api/courses/{courseId}/students: List students enrolled in a course.GET /api/courses/filter?category_id={id}&search={term}: Filter courses by category and name.
Testing
- Unit Tests: Run
php artisan testto verify repository functionality. Tests confirm courses are fetched with category and enrollment data. - Postman Tests:
- List All Courses: Verifies courses include
category_nameandenrollments_count, using DTOs and eager loading. - List Students in Course: Verifies students for a given course are returned with only
id,name, andemail. If empty, check enrollment data. - Filter Courses: Verifies filtering by
category_id,search, or both, with correct DTO structure. Ensure validcategory_id.
- List All Courses: Verifies courses include
- Query Optimization: Eager loading (
with,withCount) and selective field loading (select) ensure no N+1 issues.
Project Structure
app/Models: Eloquent models for entities.app/Repositories: Repository pattern implementation.app/DTOs: Data Transfer Objects.app/Http/Controllers: API controllers.routes/api.php: API route definitions.tests/Unit: Unit tests for repositories.