Skip to main content

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

The Eloquent model class (used for model name extraction).

service

The service instance handling business logic.

Core Methods

process_request()

Extracts and normalizes query parameters from the request.
Request
required
Laravel HTTP request object
array
Normalized parameters object:
Merging behavior:
  • Merges query string and request body parameters
  • attr and eq are merged if both present
  • All parameters are optional (null if not provided)
Example request:

handleDatabaseException()

Parses database exceptions into user-friendly error messages.
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”)
Handles:
  • Foreign key violations
  • Unique constraint violations
  • NOT NULL violations
  • Syntax errors
  • Connection errors
Logging: Full exception details are logged to the rest-generic-class channel.

callAction()

Intercepts method calls for logging (if enabled). Configuration:
Log location: storage/logs/query.log

REST Endpoints

index()

HTTP: 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
Example:

getOne()

HTTP: GET /resource/first Retrieves the first record matching filters.
mixed
Same query parameters as index()
array

show()

HTTP: 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
Example:

store()

HTTP: POST /resource Creates a new resource (or multiple).
object
required
Resource attributes OR wrapped array:
object
object
Validation failed
Transaction handling:
  • Automatically wraps in database transaction
  • Commits on success
  • Rolls back on error
Example:

update()

HTTP: 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
Example:

updateMultiple()

HTTP: PATCH /resource/batch Updates multiple resources at once.
array
required
Array of objects with IDs:
object

destroy()

HTTP: DELETE /resource/{id} Deletes a resource by ID.
mixed
required
Resource ID
object
object
Resource not found
Example:

deleteById()

HTTP: DELETE /resource/batch Deletes multiple resources by IDs.
array
required
Array of IDs to delete
object

Export Endpoints

export_excel()

HTTP: 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
Example:

export_pdf()

HTTP: 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()

HTTP: 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

That’s it! You now have a complete REST API with:
  • 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:

BaseModel

Model class with validation and hierarchy

BaseService

Service layer handling business logic

Filter Syntax

Complete filter operators reference

Middleware

Authentication and authorization