Skip to content

DannyBoyIL/BrainPOP-API-System

Repository files navigation

BrainPOP — Laravel REST API (PHP · Laravel 8 · Passport · Eloquent)

A portfolio REST API built with Laravel 8, demonstrating a relational domain model across three entities, resource-based routing, standardised JSON response patterns, and token authentication via Laravel Passport.

Domain Model

The API models an educational platform built around three core entities and their relationships:

Teacher
  └── has many Periods
        └── has many Students

Each entity supports full CRUD operations through dedicated controllers and API Resources. The relational structure reflects a real-world educational system — a teacher owns multiple class periods, each period enrolls multiple students — making this a meaningful demonstration of Eloquent relationships and layered architecture beyond single-table CRUD.

Highlights

  • Relational domain model — Teacher → Period → Student across 3 Eloquent models with defined relationships
  • API Resources — consistent JSON response shaping via PeriodResource, TeacherResource, StudentResource
  • Standardised response layer — base sendResponse() and sendError() helpers for uniform API output across all endpoints
  • Input validation — Laravel Validator class used at the controller level before any persistence logic
  • Token authentication — Laravel Passport integrated for API token-based registration and login
  • RESTful routing — clean resource routes via Route::apiResource() across all three domains
  • Environment-based configuration.env.example provided for straightforward local setup

Tech Stack

  • PHP 7.3 / 8.0
  • Laravel 8
  • Laravel Passport
  • Eloquent ORM
  • MySQL / SQLite
  • Guzzle 7

Quick Start

# 1. Clone the repository
git clone https://github.com/DannyBoyIL/BrainPOP.git
cd BrainPOP

# 2. Install dependencies
composer install

# 3. Configure environment
cp .env.example .env
php artisan key:generate

# 4. Set up database (update DB_* in .env first)
php artisan migrate

# 5. Install Passport keys
php artisan passport:install

# 6. Start the server
php artisan serve

API available at: http://localhost:8000/api

API Endpoints

Authentication

Method Endpoint Description
POST /api/register Register a new user
POST /api/login Login and receive token

Teachers

Method Endpoint Description
GET /api/teachers List all teachers
POST /api/teachers Create a teacher
GET /api/teachers/{id} Get a teacher
PUT /api/teachers/{id} Update a teacher
DELETE /api/teachers/{id} Delete a teacher

Periods

Method Endpoint Description
GET /api/periods List all periods
POST /api/periods Create a period
GET /api/periods/{id} Get a period
PUT /api/periods/{id} Update a period
DELETE /api/periods/{id} Delete a period

Students

Method Endpoint Description
GET /api/students List all students
POST /api/students Create a student
GET /api/students/{id} Get a student
PUT /api/students/{id} Update a student
DELETE /api/students/{id} Delete a student

Response Structure

All responses follow a consistent envelope via the base response helpers:

Success:

{
  "success": true,
  "data": { ... },
  "message": "Period updated successfully."
}

Error:

{
  "success": false,
  "message": "Validation Error.",
  "data": {
    "name": ["The name field is required."]
  }
}

Validation Pattern

Validation is handled at the controller level using Laravel's Validator class before any update or persistence logic runs:

public function update(Request $request, Period $period): JsonResponse
{
    $validator = Validator::make($request->all(), [
        'name' => 'required'
    ]);

    if (!$validator->fails()) {
        if ($period->update($request->all())) {
            return $this->sendResponse(new PeriodResource($period), 'Period updated successfully.');
        }
    }

    return $this->sendError('Validation Error.', $validator->errors());
}

Why Validator Over FormRequest

Laravel's FormRequest classes are the idiomatic approach for larger applications. This project uses Validator::make() directly to keep the validation logic visible and explicit within the controller — making the flow easier to follow and the design decisions easier to discuss.

Project Structure

app/
├── Http/
│   ├── Controllers/API/     # Resource controllers (Teacher, Period, Student, Auth)
│   └── Resources/           # API Resource transformers
├── Models/                  # Eloquent models with relationships
└── Providers/               # Service providers including Passport

database/
├── migrations/              # Schema definitions for all three entities
└── seeders/                 # Development seed data

routes/
└── api.php                  # RESTful API route definitions

Author

Daniel Lotemgithub.com/DannyBoyIL · linkedin.com/in/daniel-lotem

About

Laravel-based API system built as a technical assessment, focusing on relational design and structured JSON responses.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors