Skip to main content

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

Model|string
required
The Eloquent model class (instance or class name string)
Example:

Query Methods

list_all()

Retrieves all records matching query parameters.
array
required
Query parameters object with the following optional keys:
  • relations: Array or JSON string of relations to eager load
  • select: Fields to select (array or comma-separated string)
  • attr / eq: Equality filters (deprecated, use oper instead)
  • 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
bool
default:"true"
Return JSON-serializable array (vs raw collection)
array|LengthAwarePaginator|CursorPaginator
Results depend on pagination:
  • No pagination: ['data' => [...]]
  • With pagination: LengthAwarePaginator or CursorPaginator
  • Hierarchy mode: Nested tree structure
Example:

get_one()

Retrieves first record matching query parameters.
array
required
Same parameters as list_all() (pagination ignored)
bool
default:"true"
Return JSON-serializable array
array
['data' => Model|null]
Example:

show()

Retrieves a single record by ID with optional relations.
array
required
  • relations: Relations to load
  • select: Fields to select
  • hierarchy: Hierarchy mode (see showHierarchy())
mixed
required
Primary key value
Model|array
Model instance or hierarchical array structure
Example:

CRUD Methods

create()

Creates one or more records.
array
required
Single record attributes OR wrapped array:
array
Example:

update()

Updates a single record by ID.
array
required
Fields to update (partial updates supported)
mixed
required
Primary key or fieldKeyUpdate value
bool
default:"false"
Run validation before saving
array
Example:

update_multiple()

Updates multiple records at once.
array
required
Array of records with primary keys:
bool
default:"false"
Validate each record before saving
array

destroy()

Deletes a record by ID.
mixed
required
Primary key value
array
Example:

destroybyid()

Deletes records using Eloquent’s static destroy() method (supports multiple IDs).
mixed|array
required
Single ID or array of IDs
Example:

Hierarchy Methods

showHierarchy()

Retrieves a record in hierarchical structure.
array
required
Query params with hierarchy config:
mixed
required
Record ID
array
Nested tree structure based on mode:
  • node_only: Just the node with empty children array
  • with_descendants: Node + all descendants as nested tree
  • with_ancestors: Chain from root down to node
  • full_branch: Entire branch (ancestors + node + descendants)
Example:

Export Methods

exportExcel()

Exports query results to Excel file.
array
required
Same query params as list_all() plus:
  • filename: Output filename (default: excel.xlsx)
  • columns: Columns to include (default: uses select or all fillable)
BinaryFileResponse
Excel file download response
Example:

exportPdf()

Exports query results to PDF file.
array
required
Same query params plus:
  • filename: Output filename (default: pdf_file.pdf)
  • template: Blade view name (default: pdf)
  • columns: Columns to include
Response
PDF file download response
Example:

Filter Syntax (oper)

The oper parameter supports advanced filtering with logical operators and conditions.

Structure

Supported Operators

  • =, !=, <, >, <=, >=
  • like, not like, ilike, not ilike
  • in, not in
  • between, not between
  • date, not date
  • null, not null
  • exists, not exists
  • regexp, not regexp

Examples

Pagination

Standard Pagination

Cursor Pagination (Infinite Scroll)

Caching

BaseService includes a built-in caching layer with versioning.

Configuration

In config/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.
Important: Foreign keys are automatically included even if not specified.

Nested Relations

Complete Example

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