Overview
BaseService is the heart of the package’s query processing engine. It provides:
- Advanced query building: Dynamic filters, relations, pagination, ordering
- CRUD operations: Create, read, update, delete with validation
- Caching layer: Configurable caching with versioning and invalidation
- Hierarchy support: Tree-structured data retrieval
- Export functionality: Excel and PDF generation
- Relation filtering: Nested queries with
whereHas
Create a service for each model by extending
BaseService and passing the model class to the constructor.Constructor
The Eloquent model class (instance or class name string)
Query Methods
list_all()
Query parameters object with the following optional keys:
relations: Array or JSON string of relations to eager loadselect: Fields to select (array or comma-separated string)attr/eq: Equality filters (deprecated, useoperinstead)oper: Advanced filter conditions (see Filter Syntax)orderby: Array of['field' => 'asc'|'desc']pagination: Pagination config (see Pagination)hierarchy: Hierarchy mode config (see Hierarchy)_nested: If true, applies relation filters to eager loading
Return JSON-serializable array (vs raw collection)
Results depend on pagination:
- No pagination:
['data' => [...]] - With pagination:
LengthAwarePaginatororCursorPaginator - Hierarchy mode: Nested tree structure
get_one()
Same parameters as
list_all() (pagination ignored)Return JSON-serializable array
['data' => Model|null]show()
relations: Relations to loadselect: Fields to selecthierarchy: Hierarchy mode (seeshowHierarchy())
Primary key value
Model instance or hierarchical array structure
CRUD Methods
create()
Single record attributes OR wrapped array:
update()
Fields to update (partial updates supported)
Primary key or
fieldKeyUpdate valueRun validation before saving
update_multiple()
Array of records with primary keys:
Validate each record before saving
destroy()
Primary key value
destroybyid()
destroy() method (supports multiple IDs).
Single ID or array of IDs
Hierarchy Methods
showHierarchy()
Query params with
hierarchy config:Record ID
Nested tree structure based on
mode:node_only: Just the node with empty children arraywith_descendants: Node + all descendants as nested treewith_ancestors: Chain from root down to nodefull_branch: Entire branch (ancestors + node + descendants)
Export Methods
exportExcel()
Same query params as
list_all() plus:filename: Output filename (default:excel.xlsx)columns: Columns to include (default: usesselector all fillable)
Excel file download response
exportPdf()
Same query params plus:
filename: Output filename (default:pdf_file.pdf)template: Blade view name (default:pdf)columns: Columns to include
PDF file download response
Filter Syntax (oper)
Theoper parameter supports advanced filtering with logical operators and conditions.
Structure
Supported Operators
=,!=,<,>,<=,>=like,not like,ilike,not ilikein,not inbetween,not betweendate,not datenull,not nullexists,not existsregexp,not regexp
Examples
Pagination
Standard Pagination
Cursor Pagination (Infinite Scroll)
Caching
BaseService includes a built-in caching layer with versioning.Configuration
Inconfig/rest-generic-class.php:
Cache Invalidation
Cache is automatically invalidated (version bumped) after:create()update()update_multiple()destroy()destroybyid()
Per-Request Control
Relations with Field Selection
Load relations with specific fields to optimize queries.Nested Relations
Complete Example
Related
BaseModel
Model class with validation and hierarchy support
RestController
Controller exposing service methods as REST endpoints
Filter Syntax
Complete guide to filter operators and syntax