Skip to main content

Overview

The BaseService class handles all business logic for REST operations. It sits between your controller and model, providing:
  • CRUD operations with validation
  • Dynamic filtering via the oper parameter
  • 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 extending BaseService:
That’s it! Your service now has all REST capabilities.

Core Methods

list_all()

Retrieves a collection of records with optional filtering, relations, and pagination.
array
Query parameters from the request:
  • select — Column selection
  • relations — Eager load relations
  • oper — Filters (and/or conditions)
  • orderby — Sorting
  • pagination — Page settings
  • hierarchy — Hierarchical mode
  • _nested — Apply relation filters to eager loads
bool
default:"true"
If true, returns ['data' => $results]. If false, returns raw array.
Example:

get_one()

Retrieves a single record matching the query.
Same parameters as list_all(), but returns the first matching record:

create()

Creates one or more records.
Single record:
Batch creation:
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.
Example:
Partial updates are supported — only send the fields you want to change:

destroy()

Deletes a record by ID.
Supports soft deletes if your model uses SoftDeletes trait.

Query Building with process_query()

The process_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

  1. Legacy filters (attr) — Simple equality conditions
  2. Dynamic filters (oper) — Complex filtering tree with whereHas for relations
  3. Eager loading (relations) — Loads related data (optionally filtered if _nested=true)
  4. Column selection (select) — Limits returned columns
  5. Sorting (orderby) — Applies ORDER BY clauses
  6. Pagination — Applied by process_all() after process_query()

Filtering System

The service uses HasDynamicFilter trait for powerful query building.

Filter Operators

From HasDynamicFilter.php:35-57:

Filter Format

Conditions use pipe-separated format:
Example filters:
See Dynamic Filtering for complete documentation.

Relation Loading

The relations() method handles eager loading with optional field selection. Basic syntax:
With field selection:
Nested relations:
See Relation Loading for advanced usage.

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)
From BaseService.php:1127-1151:

Cache Invalidation

The cache version bumps automatically after write operations (create, update, destroy), invalidating all cached queries for that model.

Per-Request Control

Or set custom TTL:

Export Helpers

Export features require optional packages:

exportExcel()

exportPdf()

The Blade template receives:
  • $data — Array of records
  • $columns — Column names to display
  • $model — Model instance
  • $params — Original parameters

Complete Example

Next Steps