Skip to main content

Overview

The RestController 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 extending RestController:
That’s it! Your controller now has all REST endpoints.

Route Registration

Register the controller in your routes file:
Or use resource routing:

Available Endpoints

index() — List Records

Retrieves a paginated list of records with filtering, relations, and sorting.
Request example:
Response:
Or with pagination:

getOne() — Get Single Record by Query

Retrieves the first record matching the query parameters.
Request example:
Response:

show() — Get Record by ID

Retrieves a single record by its primary key.
Request example:
Response:
show() supports the same relations, select, and hierarchy parameters as index().

store() — Create Record

Creates a new record with validation.
Request example:
Response:
Batch creation:
The key name (product) must match your model’s const MODEL value.

update() — Update Record

Updates an existing record by ID.
Request example:
Response:
Partial updates are supported — only send the fields you want to change.

destroy() — Delete Record

Deletes a record by ID.
Request example:
Response:

Request Processing

The process_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 via DatabaseErrorParser. 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”
Example error response:

Transaction Management

Write operations automatically use database transactions. From RestController.php:158-176 (store method):
All write operations (store, update, updateMultiple, destroy) follow this pattern:
  1. Begin transaction
  2. Execute operation
  3. Commit if successful
  4. Rollback on error
  5. Log failures

Validation Integration

Use BaseFormRequest for custom validation:
Update your controller:
Now validation runs automatically before store() executes.

Bulk Operations

updateMultiple()

Update multiple records in one request.
Request example:
Response:

deleteById()

Delete multiple records by IDs.
Request example:

Export Endpoints

export_excel()

Returns an Excel file download.

export_pdf()

Returns a PDF file download using the specified Blade template.

Complete Example

Next Steps