Overview
TheBaseService class handles all business logic for REST operations. It sits between your controller and model, providing:
- CRUD operations with validation
- Dynamic filtering via the
operparameter - Eager loading with field selection
- Pagination (standard and cursor-based)
- Hierarchical queries for tree data
- Automatic caching (optional)
- Export helpers for Excel and PDF
Service Setup
Create a service for each model by extendingBaseService:
Core Methods
list_all()
Retrieves a collection of records with optional filtering, relations, and pagination.array
Query parameters from the request:
select— Column selectionrelations— Eager load relationsoper— Filters (and/or conditions)orderby— Sortingpagination— Page settingshierarchy— Hierarchical mode_nested— Apply relation filters to eager loads
bool
default:"true"
If
true, returns ['data' => $results]. If false, returns raw array.get_one()
Retrieves a single record matching the query.list_all(), but returns the first matching record:
create()
Creates one or more records.The key name (
product) must match your model’s const MODEL value.update()
Updates an existing record.array
Fields to update (only provided fields are changed).
mixed
The primary key value or custom field key if
fieldKeyUpdate is set.bool
default:"false"
If
true, runs model validation before saving.destroy()
Deletes a record by ID.SoftDeletes trait.
Query Building with process_query()
Theprocess_query() method is the heart of the service layer. It transforms REST parameters into an Eloquent query.
From BaseService.php:350-398:
Order of Execution
- Legacy filters (
attr) — Simple equality conditions - Dynamic filters (
oper) — Complex filtering tree withwhereHasfor relations - Eager loading (
relations) — Loads related data (optionally filtered if_nested=true) - Column selection (
select) — Limits returned columns - Sorting (
orderby) — AppliesORDER BYclauses - Pagination — Applied by
process_all()afterprocess_query()
Filtering System
The service usesHasDynamicFilter trait for powerful query building.
Filter Operators
From HasDynamicFilter.php:35-57:Filter Format
Conditions use pipe-separated format:Relation Loading
Therelations() method handles eager loading with optional field selection.
Basic syntax:
Caching
BaseService includes built-in caching for read operations.Configuration
From config/rest-generic-class.php:Cache Keys
Cache keys are deterministic and include:- Operation name (
list_all,get_one) - Model class
- Query parameters (
select,oper,relations, etc.) - User ID (from
auth()->id()) - Vary headers (locale, tenant, etc.)
- Cache version (for invalidation)
Cache Invalidation
The cache version bumps automatically after write operations (create, update, destroy), invalidating all cached queries for that model.
Per-Request Control
Export Helpers
exportExcel()
exportPdf()
$data— Array of records$columns— Column names to display$model— Model instance$params— Original parameters
Complete Example
Next Steps
- Controllers — Connect services to REST endpoints
- Dynamic Filtering — Master the
operparameter - Relation Loading — Advanced eager loading techniques
- Hierarchical Data — Work with tree structures