Overview
TheRestController class provides a complete REST API interface with minimal configuration. It handles:
- Request parameter processing (select, relations, oper, pagination, etc.)
- Standard CRUD endpoints (index, show, store, update, destroy)
- Validation integration via FormRequest classes
- Database error handling with user-friendly messages
- Transaction management for write operations
- Export endpoints for Excel and PDF
Controller Setup
Create a controller by extendingRestController:
Route Registration
Register the controller in your routes file:Available Endpoints
index() — List Records
Retrieves a paginated list of records with filtering, relations, and sorting.getOne() — Get Single Record by Query
Retrieves the first record matching the query parameters.show() — Get Record by ID
Retrieves a single record by its primary key.show() supports the same relations, select, and hierarchy parameters as index().store() — Create Record
Creates a new record with validation.update() — Update Record
Updates an existing record by ID.destroy() — Delete Record
Deletes a record by ID.Request Processing
Theprocess_request() method extracts and normalizes query parameters.
From RestController.php:79-102:
Supported Parameters
array|string
Columns to return. Example:
["id","name","price"] or "*"array
Relations to eager load. Example:
["category:id,name","reviews"]object|array
Dynamic filters. Example:
{"and":["status|=|active"]}array
Sorting rules. Example:
[{"price":"desc"},{"name":"asc"}]object
Page settings. Example:
{"page":1,"pageSize":25}bool|object
Hierarchical mode. Example:
true or {"filter_mode":"with_descendants","max_depth":3}bool
default:"false"
Apply relation filters to eager loads.
object
Legacy equality filters. Example:
{"status":"active","category_id":5}Error Handling
RestController includes automatic database error handling viaDatabaseErrorParser.
From RestController.php:58-72:
Error Types
The parser detects and translates common database errors:- Foreign key constraint violations → “Cannot delete: record is referenced by other records”
- Unique constraint violations → “Duplicate value for field ‘email’”
- Not null violations → “Field ‘name’ is required”
- Connection errors → “Database connection failed”
Transaction Management
Write operations automatically use database transactions. From RestController.php:158-176 (store method):store, update, updateMultiple, destroy) follow this pattern:
- Begin transaction
- Execute operation
- Commit if successful
- Rollback on error
- Log failures
Validation Integration
UseBaseFormRequest for custom validation:
store() executes.
Bulk Operations
updateMultiple()
Update multiple records in one request.deleteById()
Delete multiple records by IDs.Export Endpoints
export_excel()
export_pdf()
Complete Example
Next Steps
- Dynamic Filtering — Master the
operparameter - Relation Loading — Advanced eager loading
- Hierarchical Data — Work with tree structures