Skip to main content

Overview

Rest Generic Class provides a flexible dynamic filtering system that allows clients to build complex queries using the oper parameter. The system supports:
  • Multiple comparison operators (equality, range, pattern matching)
  • Logical grouping with and/or conditions
  • Nested filter structures for advanced queries
  • Automatic table prefixing to avoid column ambiguity
  • Database-specific features (PostgreSQL ilike, unaccent)

Basic Filter Structure

Filters are passed via the oper query parameter as a JSON object with logical operators:
Each condition follows the format: field|operator|value

Supported Operators

The filtering system supports a comprehensive set of operators:

Comparison Operators

Pattern Matching

List Operators

Range Operators

Null Operators

Date Operators

Existence Operators

Logical Operators

AND Conditions

All conditions must be true:

OR Conditions

At least one condition must be true:

Nested Conditions

Combine AND/OR logic for complex queries:
This creates SQL equivalent to:

Value Types

The filter system automatically decodes values:

Configuration

Configure filtering behavior in config/rest-generic-class.php:

HasDynamicFilter Trait

The filtering logic is implemented in the HasDynamicFilter trait, which provides:

scopeWithFilters

Apply filters to an Eloquent query:

Key Methods

Advanced Examples

Search with Multiple Conditions

Complex Business Logic

Find active products that are either featured OR low in stock:

Date Range Filtering

Security Considerations

The filtering system includes several protections:
  • Operator allowlist prevents SQL injection via invalid operators
  • Column validation ensures only valid table columns are queried
  • Max depth/conditions prevents DoS attacks with overly complex queries
  • Prepared statements all values are properly escaped
  • Relation restrictions only declared relations can be queried

Performance Tips

  1. Index filtered columns: Add database indexes to commonly filtered fields
  2. Limit nesting depth: Keep filter structures simple when possible
  3. Use specific operators: = is faster than like
  4. Avoid leading wildcards: name|like|%term is slower than name|like|term%
  5. Enable column caching: Reduces validation overhead

Error Handling

Common filtering errors: