sopheak / sp-laravel-api
Core utilities for Laravel apps: standardized API responses, request ID middleware, and CLI helpers.
Requires
- php: ^8.2|^8.3|^8.4|^8.5
- intervention/image: ^4.0
- laravel/framework: ^12.0|^13.0
- nesbot/carbon: ^2.0|^3.0
Requires (Dev)
- aws/aws-sdk-php: ^3.0
- friendsofphp/php-cs-fixer: ^3.92
- larastan/larastan: ^3.0
- laravel/ai: ^1.0.1
- laravel/mcp: ^1.0.1
- league/flysystem-aws-s3-v3: ^3.0
- orchestra/testbench: ^10.0|^11.0
- phpstan/phpstan: 2.1.32
- phpunit/phpunit: ^10.0|^11.0
- rector/rector: ^2.0
Suggests
- aws/aws-sdk-php: Required for S3/R2 presigned and multipart direct uploads.
- laravel/ai: Required for the AI SDK record tools (PHP 8.3+).
- laravel/mcp: Required for the `laravel` MCP driver (record.mcp.driver = laravel).
- league/flysystem-aws-s3-v3: Required for S3/R2 filesystem disks.
Provides
None
Conflicts
None
Replaces
None
- dev-main
- 0.5.04
- 0.5.02
- 0.5.01
- 0.4.99
- 0.4.98
- 0.4.97
- 0.4.96
- 0.4.95
- 0.4.94
- 0.4.93
- 0.4.92
- 0.4.90
- 0.4.89
- 0.4.88
- 0.4.87
- 0.4.86
- 0.4.85
- 0.4.84
- 0.4.83
- 0.4.82
- 0.4.80
- 0.4.79
- 0.4.77
- 0.4.76
- 0.4.75
- 0.4.66
- 0.4.65
- 0.4.64
- 0.4.62
- 0.4.61
- 0.4.60
- 0.4.50
- 0.4.43
- 0.4.41
- 0.4.40
- 0.4.35
- 0.4.34
- 0.4.33
- 0.4.32
- 0.4.31
- 0.4.30
- 0.4.22
- 0.4.21
- 0.4.20
- 0.4.10
- 0.4.0
- 0.3.97
- 0.3.96
- 0.3.94
- 0.3.94-beta.41
- 0.3.93
- 0.3.91
- 0.3.90
- 0.3.87
- 0.3.86
- 0.3.83
- 0.3.81
- 0.3.8
- 0.3.7
- 0.3.6
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.99
- 0.2.98
- 0.2.97
- 0.2.96
- 0.2.95
- 0.2.94
- 0.2.93
- 0.2.90
- 0.2.89
- 0.2.88
- 0.2.87
- 0.2.86
- 0.2.85
- 0.2.83
- 0.2.82
- 0.2.81
- 0.2.80
- 0.2.74
- 0.2.73
- 0.2.72
- 0.2.71
- 0.2.70
- 0.2.64
- 0.2.63
- 0.2.62
- 0.2.41
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.99
- 0.1.98
- 0.1.97
- 0.1.96
- 0.1.93
- 0.1.92
- 0.1.91
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.0
- dev-develop
- dev-fix/scaffold-validator-id-type
- dev-feature/config-namespace-prefix
- dev-fix/id-type-followups
- dev-feature/configurable-id-type
- dev-role-pms
- dev-claude/quizzical-lamport
This package is auto-updated.
Last update: 2026-10-08 03:20:01 UTC
README
sopheak/sp-laravel-api is a comprehensive, config-driven REST API package for Laravel applications. It replaces repetitive controllers, boilerplate query builders, and manual CRUD endpoints with declarative schema definitions while providing standardized API responses, multi-tenant isolation, granular field permissions, transactional audit trails, file attachment workflows, and AI/MCP tool integrations.
The package owns API infrastructure, dynamic endpoint resolution, query filtering, and audit logging. Your application defines the table schemas, business rules, custom functions, and authorization policies.
Public package links:
- Documentation: sp-laravel-api-docs.vercel.app
- Packagist: packagist.org/packages/sopheak/sp-laravel-api
- Canonical Repository: github.com/sopheaksem9999/sp-laravel-unified-api
Features
| Feature | What it provides | Default | Guide |
|---|---|---|---|
| Config-Driven Dynamic CRUD | Declarative RecordTableType schema; automatic GET, POST, PUT, DELETE, and atomic upsert endpoints |
Enabled | CRUD Operations |
| Standardized API Envelope | Consistent JSON response format ({ success, error_code, data, meta }), request IDs, and microsecond execution timing |
Enabled | API Responses |
| Multi-Tenant Isolation | Automatic tenant scoping via X-Tenant-ID header across queries, includes, bulk operations, and cache namespaces |
Enabled | Tenant Isolation |
| Advanced Query Filtering | PostgREST-style operators (eq, neq, like, in, gt, gte, between, is_null), sorting, and field projection |
Enabled | Query Filters |
| Relational Includes | Subquery loading and JOIN resolution via select= query syntax (RecordHasManyType, RecordBelongsToType, etc.) |
Enabled | Relationships |
| Pagination Engine | Offset-based (page/per_page) and cursor-based pagination for high-volume datasets |
Enabled | Pagination |
| Bulk Operations | High-throughput batch create, update, delete, and upsert with queue support | Enabled | Bulk Operations |
| Permissions & RLS Scoping | Column hidden lists (columnHiddens), operation guards (canRead, canCreate), viewOwn scoping, and Laravel Gate integration |
Enabled | Permissions |
| Transactional Audit Logging | Comprehensive change tracking (old/new diffs, actor ID, client IP, user agent, tenant ID) with queue buffering | Enabled | Audit Logging |
| Attachments & Direct Upload | S3/Cloudflare R2 direct uploads, multipart upload, presigned private preview URLs, and image resizing | Configurable | Attachments |
| Real-time OpenAPI Generator | Dynamic OpenAPI 3.0 specification auto-generated from active table types and custom function attributes | Enabled | OpenAPI Docs |
| API Client Exporters | Instant export to Bruno (.bru) and Postman collection files with auth header management |
Enabled | API Clients |
| Table Triggers & Hooks | Lifecycle hooks (beforeCreate, afterUpdate, etc.) and database triggers for custom domain rules |
Configurable | Record Hooks |
| AI SDK & MCP Record Tools | First-class AI tools and Model Context Protocol (MCP) server driver (laravel/mcp, laravel/ai) for agentic workflows |
Optional | AI & MCP |
| Cache Management | High-performance per-table and query caching with automatic cache invalidation on writes | Optional (Opt-in) | Cache Guide |
Core package features
The enabled-by-default core provides enterprise SaaS applications with a unified API layer while leaving application-specific business logic in your app:
- Config-Driven Architecture — Define table schemas in
config/records/tables/*.phpwithout repetitive boilerplate controllers, requests, or repository classes. - Unified Response Contract — Every endpoint returns
{ success, error_code, data, meta }with automatic error normalization, validation errors, andX-Request-IDcorrelation. - Built-in Multi-Tenancy — Strict tenant isolation via
X-Tenant-IDheader, preventing cross-tenant leakage across reads, writes, nested relationships, and cache tags. - Granular Authorization — Configure table-level permissions via
isAuthRead/isAuthWrite, action availability (canRead,canCreate,canUpdate,canDelete,canUpsert), column hiding, andviewOwnownership scoping. - Full Observability & Audit Trail — Automatically record all state mutations with before/after state diffs, authenticated userstamps, IP addresses, and user agents.
- AI & MCP Native — Expose your dynamic tables directly to LLM agents using Laravel MCP (
laravel/mcp) or Laravel AI SDK (laravel/ai) tools. - Operational Tooling — CLI commands to scaffold schemas, validate configuration, and export OpenAPI 3.0 specifications and Bruno/Postman collections.
🚀 Quick Start
Requirements
- PHP:
^8.2,^8.3,^8.4, or^8.5(PHP 8.3+ required for AI SDK tools) - Laravel:
^12.0or^13.0 - Database: MySQL 8.0+, PostgreSQL 13+, or SQLite
Installation
-
Install the package via Composer:
composer require sopheak/sp-laravel-api
-
Publish package configuration and migrations:
php artisan sp-laravel-api:setup
-
Run the database migrations:
php artisan migrate
-
Validate the installation configuration:
php artisan sp-laravel-api:validate
⚙️ Configuration Example
Define a table configuration in config/records/tables/orders.php:
<?php declare(strict_types=1); use Sopheak\Core\Types\RecordBelongsToType; use Sopheak\Core\Types\RecordHasManyType; use Sopheak\Core\Types\RecordTableType; return new RecordTableType( pmsName: 'order', table: 'orders', isAuthRead: true, isAuthWrite: true, canRead: true, canCreate: true, canUpdate: true, canDelete: true, canUpsert: true, relationships: [ 'customer' => new RecordBelongsToType( table: 'customers', foreignKey: 'customer_id', ownerKey: 'id', ), 'items' => new RecordHasManyType( table: 'order_items', foreignKey: 'order_id', localKey: 'id', ), ], );
Dynamic Endpoints Generated
Once the table config is defined, the package instantly generates standard RESTful endpoints:
| Method | Endpoint | Description |
|---|---|---|
GET |
/api/records/orders |
List records with filtering, sorting, pagination, and includes |
GET |
/api/records/orders/{id} |
Retrieve a single record by ID |
POST |
/api/records/orders |
Create a new record |
PUT |
/api/records/orders/{id} |
Update an existing record |
DELETE |
/api/records/orders/{id} |
Delete a record |
POST |
/api/records/orders/upsert |
Atomically insert or update records |
Querying with Filters & Relationships
# Filter orders by status, eager load customer and items, 15 per page: curl -X GET "https://api.example.com/api/records/orders?filter[status]=completed&select=customer,items&per_page=15" \ -H "Authorization: Bearer <token>" \ -H "X-Tenant-ID: tenant-123"
Standard Response Structure
{
"success": true,
"error_code": 0,
"data": [
{
"id": 101,
"order_number": "ORD-2026-001",
"status": "completed",
"customer": {
"id": 42,
"name": "Acme Corp"
},
"items": [
{
"id": 201,
"product_name": "Premium License",
"quantity": 1
}
]
}
],
"meta": {
"total": 1,
"page": 1,
"per_page": 15,
"request_id": "req_65b3f2e1a9c4"
}
}
🛠️ Artisan CLI Tooling
The package provides Artisan commands for developer workflows:
| Command | Description |
|---|---|
php artisan sp-laravel-api:setup |
Publish package configuration and database migrations |
php artisan sp-laravel-api:record {name} |
Scaffold a new table configuration schema |
php artisan sp-laravel-api:validate |
Validate all active table definitions and relationships |
php artisan sp-laravel-api:export-openapi |
Export full OpenAPI 3.0 specification file |
php artisan sp-laravel-api:export-bruno |
Export Bruno (.bru) API client collection |
php artisan sp-laravel-api:export-postman |
Export Postman API client collection |
php artisan sp-laravel-api:agent |
Scaffold AI agent skills, rules, and MCP configuration |
📚 Documentation
For complete guides, interactive examples, architecture overviews, and API references, visit:
- Official Documentation Website: https://sp-laravel-api-docs.vercel.app/
- Documentation Repository: github.com/sopheaksem9999/sp-laravel-api-docs
- Contributing Guidelines
📄 License
This package is proprietary software. All rights reserved.