This project implements a simple RESTful API using Laravel, focusing on clean code, maintainability, and clarity of structure rather than complex logic correctness.
It is part of a coding test to demonstrate proper naming, code organization, and readability.
POST /api/v1/users— Create a new user.GET /api/v1/users— List all active users with pagination, search, and sorting.- Automatically sends:
- Welcome email to the new user.
- Notification email to the admin.
- Includes computed fields:
orders_count— total number of orders per user.can_edit— whether the authenticated user can edit that user record (based on role rules).
- Laravel (latest stable version)
- PHP 8.2+
- Eloquent ORM
- Laravel Mail (Mailables)
- SQLite/MySQL (configurable)
- PHPUnit for feature testing
- PHP >= 8.2
- Composer
- Laravel CLI (
artisan) - SQLite or MySQL for database
- (Optional) Mailtrap or Log driver for mail testing
git clone https://github.com/your-username/api-test.git
cd api-test
composer install
cp .env.example .env
php artisan key:generateIn .env file:
DB_CONNECTION=sqlite
DB_DATABASE=/absolute/path/to/database/database.sqlite
MAIL_MAILER=log
ADMIN_EMAIL=admin@example.com
MAIL_FROM_ADDRESS=no-reply@example.com
MAIL_FROM_NAME="API Test"Then run:
touch database/database.sqlite
php artisan migrateStart the application:
php artisan servePOST /api/v1/users
Example request body:
{
"email": "alice@example.com",
"password": "password123",
"name": "Alice",
"role": "user"
}Response (201):
{
"id": 1,
"email": "alice@example.com",
"name": "Alice",
"role": "user",
"created_at": "2025-11-13T08:00:00Z"
}GET /api/v1/users
Query parameters:
search— filter by name or email.page— pagination (default 1).sortBy— one of:name,email,created_at.
Example:
GET /api/v1/users?search=alice&sortBy=name&page=1
Response (200):
{
"page": 1,
"per_page": 15,
"total": 3,
"users": [
{
"id": 1,
"email": "alice@example.com",
"name": "Alice",
"role": "user",
"created_at": "2025-11-13T08:00:00Z",
"orders_count": 2,
"can_edit": true
}
]
}| Authenticated Role | Editable User | Condition |
|---|---|---|
| Administrator | All users | Always true |
| Manager | Users only | target.role === 'user' |
| User | Self only | target.id === actor.id |
app/
├── Http/
│ ├── Controllers/Api/UserController.php
│ ├── Requests/{StoreUserRequest, ListUserRequest}.php
│ └── Resources/UserResource.php
├── Mail/{WelcomeUser, NotifyAdmin}.php
├── Models/{User, Order}.php
routes/
└── api.php
database/
└── migrations/
tests/
└── Feature/
├── CreateUserTest.php
└── ListUsersTest.php
Run all tests:
php artisan testCreateUserTest— verifies POST /api/v1/users creates a user, sends both emails, and returns correct JSON.ListUsersTest— verifies GET /api/v1/users includes pagination, orders_count, and can_edit field logic.
You can also test this API using the included Postman collection.
- Import the file User-Management-API.postman_collection.json from this repository.
- Set the
base_urlvariable to your local environment (e.g.http://127.0.0.1:8000). - Available endpoints:
POST /api/v1/users— Create a new user.GET /api/v1/users— List users with pagination, search, and sorting.
- Follows PSR-12 and Laravel naming conventions.
- Controller methods are concise and descriptive.
- Validation handled via FormRequest.
- Responses standardized using API Resource.
- Mailables encapsulate email templates.
- Logic is separated and easily testable (e.g.
computeCanEdit()helper method).
Prastiyo Beka
GitHub · LinkedIn
The main purpose of this project is to demonstrate how I structure and organize Laravel code — focusing on clarity, maintainability, and developer readability — rather than complex business logic or full authentication layers.
All code was written manually following Laravel best practices.