Skip to main content

Overview

The HasDynamicFilter trait provides dynamic query filtering capabilities with support for complex conditions, multiple operators, and nested logic. It is used internally by BaseService but can also be used directly in custom queries. Namespace: Ronu\RestGenericClass\Core\Traits\HasDynamicFilter Location: /src/Core/Traits/HasDynamicFilter.php:11

Usage

Methods

scopeWithFilters

Apply dynamic filters to a query builder instance using a structured array.

Parameters

  • Builder $query - The Eloquent query builder instance
  • array $params - Filter structure with logical operators and conditions
  • string $condition - Default logical operator: 'and' or 'or' (default: 'and')
  • mixed $model - Model instance or class for table prefixing (optional)

Returns

Builder - The modified query builder instance

Example

applyFilters (Private)

Core filtering logic that processes filter arrays and applies them to the query.
Features:
  • Parses JSON strings or arrays
  • Validates query builder instance
  • Determines table name for column prefixing
  • Detects database driver (PostgreSQL features)
  • Processes nested filter structures
  • Validates operators against allowlist
Throws: HttpException (501, 400) for invalid input

Supported Operators

The trait supports the following operators (configurable via allowed_operators in config):

Comparison

  • = - Equals
  • !=, <> - Not equals
  • < - Less than
  • > - Greater than
  • <= - Less than or equal
  • >= - Greater than or equal

Pattern Matching

  • like - Case-sensitive pattern match
  • not like - Negated pattern match
  • ilike - Case-insensitive (PostgreSQL)
  • not ilike - Negated case-insensitive
  • ilikeu - Case-insensitive with unaccent (PostgreSQL)
  • regexp - Regular expression match
  • not regexp - Negated regex

List Operations

  • in - Value in list
  • not in, notin - Value not in list

Range Operations

  • between - Value between two values
  • not between, notbetween - Value outside range

Null Operations

  • null - Field is NULL
  • not null, notnull - Field is not NULL

Existence

  • exists - Subquery exists
  • not exists, notexists - Subquery does not exist

Date Operations

  • date - Date equals
  • not date, notdate - Date not equals

Helper Methods

getTableName (Private)

Extracts table name from query or model for column prefixing.

Parameters

  • Builder $query - Query builder instance
  • mixed $model - Model instance or class name

Returns

?string - Table name or null if unable to determine

prefixColumn (Private)

Prefixes column with table name to avoid ambiguity in joins.

Parameters

  • string $column - Column name
  • ?string $tableName - Table name for prefixing

Returns

string - Prefixed column name (e.g., products.status) Logic:
  • Skips if $tableName is null
  • Skips if column already contains . (already prefixed)
  • Skips if column contains ( (SQL function)
  • Otherwise prefixes: table_name.column

Example

parseConditionString (Protected)

Parses a condition string into field, operator, and value components.

Parameters

  • mixed $condition - Condition string in format field|operator|value

Returns

array - Array with three elements: [field, operator, value]

Throws

HttpException (400) if condition is not a valid string or format is incorrect

Example

decodeValue (Protected)

Decodes string values into appropriate PHP types.

Parameters

  • string $val - String value to decode

Returns

mixed - Decoded value (string, number, boolean, null, or array)

Logic

  • Splits comma-separated values into arrays
  • Converts 'null'null
  • Converts 'true'true
  • Converts 'false'false
  • Converts numeric strings → numbers
  • Otherwise returns as string

Example

toBetweenArray (Protected)

Validates and formats values for between operator.

Parameters

  • mixed $val - Value to validate (must be array with exactly 2 elements)

Returns

array - Validated two-element array

Throws

HttpException (400) if value is not an array or doesn’t have exactly 2 elements

Example

Filter Structure

Basic Structure

Nested Structure

Generates SQL:

Operator-Specific Behavior

IN Operator

BETWEEN Operator

LIKE Operator

NULL Operator

ILIKEU Operator (PostgreSQL)

Error Handling

The trait throws HttpException in the following cases:

Invalid Query Instance (501)

Invalid Operator (400)

Invalid Condition Format (400)

Invalid Condition Type (400)

Unsupported Logical Key (400)

Invalid BETWEEN Values (400)

Configuration

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

Performance Considerations

  1. Table Prefixing: Automatic prefixing helps with joins but adds minimal overhead
  2. Operator Validation: Checking allowed operators prevents injection but requires array lookup
  3. Type Conversion: decodeValue() processes each value individually
  4. Nested Filters: Deep nesting creates subqueries; limit depth via config

Security Features

Built-in Security:
  • Operator allowlist prevents SQL injection via invalid operators
  • Prepared statements used for all values
  • Column validation ensures only valid fields are queried
  • Depth/condition limits prevent DoS attacks
  • Table prefixing prevents ambiguous column attacks

Examples

Simple Filter

Multiple Conditions

Complex Nested Logic

Using with Service