Overview
RestController is the base controller class that:
- Exposes REST endpoints: Standard CRUD operations via HTTP
- Processes query parameters: Converts request params to service-compatible format
- Handles errors: Database exception parsing with user-friendly messages
- Supports exports: Excel and PDF export endpoints
- Transaction management: Automatic database transactions for write operations
- Logging: Configurable query logging
Extend
RestController and set $modelClass and $service properties to instantly create a REST API for your model.Properties
modelClass
service
Core Methods
process_request()
Request
required
Laravel HTTP request object
array
Normalized parameters object:
- Merges query string and request body parameters
attrandeqare merged if both present- All parameters are optional (null if not provided)
handleDatabaseException()
Throwable
required
Database exception (QueryException or PDOException)
DatabaseErrorParserException
Parsed exception with:
- HTTP status code
- User-friendly message
- Error type (e.g., “unique_violation”, “foreign_key_violation”)
- Foreign key violations
- Unique constraint violations
- NOT NULL violations
- Syntax errors
- Connection errors
rest-generic-class channel.
callAction()
storage/logs/query.log
REST Endpoints
index()
GET /resource
Retrieves a list of resources.
string|array
Relations to eager load (JSON array or comma-separated)
string|array
Fields to select
string|array
Filter conditions (JSON object)
string|array
Ordering (JSON array of
{"field": "direction"})string|object
Pagination config (JSON object with
page, pageSize, infinity, cursor)string|object
Hierarchy mode config
boolean
Apply relation filters to eager loading
array|LengthAwarePaginator
getOne()
GET /resource/first
Retrieves the first record matching filters.
mixed
Same query parameters as
index()array
show()
GET /resource/{id}
Retrieves a single resource by ID.
mixed
required
Resource ID
string|array
Relations to load
string|array
Fields to select
string|object
Hierarchy mode
object
object
Model not found
store()
POST /resource
Creates a new resource (or multiple).
object
required
Resource attributes OR wrapped array:
object
object
Validation failed
- Automatically wraps in database transaction
- Commits on success
- Rolls back on error
update()
PUT/PATCH /resource/{id}
Updates an existing resource.
mixed
required
Resource ID
object
required
Fields to update (partial updates supported)
object
object
Resource not found
object
Validation failed
updateMultiple()
PATCH /resource/batch
Updates multiple resources at once.
array
required
Array of objects with IDs:
object
destroy()
DELETE /resource/{id}
Deletes a resource by ID.
mixed
required
Resource ID
object
object
Resource not found
deleteById()
DELETE /resource/batch
Deletes multiple resources by IDs.
array
required
Array of IDs to delete
object
Export Endpoints
export_excel()
GET /resource/export/excel
Exports filtered data to Excel.
mixed
Same query parameters as
index() plus:string
Output filename (default:
excel.xlsx)string|array
Columns to export (default: uses
select or all fillable)binary
Excel file download
export_pdf()
GET /resource/export/pdf
Exports filtered data to PDF.
mixed
Same query parameters as
index() plus:string
Output filename (default:
pdf_file.pdf)string
Blade view name (default:
pdf)string|array
Columns to export
binary
PDF file download
Validation Endpoint
actionValidate()
POST /resource/validate
Validates request data without saving.
object
required
Data to validate
object
object
Validation errors
Error Handling
All database errors are caught and parsed into user-friendly responses.Unique Constraint Violation
Foreign Key Violation
NOT NULL Violation
Complete Implementation Example
Route Registration
- List with filtering, pagination, relations
- Single record retrieval
- Create, update, delete operations
- Batch operations
- Excel/PDF exports
- Validation endpoint
- Automatic error handling
- Transaction management
Advanced Usage
Custom Endpoints
Extend the controller with custom methods:Custom Request Validation
Create a FormRequest class:Related
BaseModel
Model class with validation and hierarchy
BaseService
Service layer handling business logic
Filter Syntax
Complete filter operators reference
Middleware
Authentication and authorization