Overview
TheHasDynamicFilter 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 instancearray $params- Filter structure with logical operators and conditionsstring $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.- 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
HttpException (501, 400) for invalid input
Supported Operators
The trait supports the following operators (configurable viaallowed_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 matchnot like- Negated pattern matchilike- Case-insensitive (PostgreSQL)not ilike- Negated case-insensitiveilikeu- Case-insensitive with unaccent (PostgreSQL)regexp- Regular expression matchnot regexp- Negated regex
List Operations
in- Value in listnot in,notin- Value not in list
Range Operations
between- Value between two valuesnot between,notbetween- Value outside range
Null Operations
null- Field is NULLnot null,notnull- Field is not NULL
Existence
exists- Subquery existsnot exists,notexists- Subquery does not exist
Date Operations
date- Date equalsnot date,notdate- Date not equals
Helper Methods
getTableName (Private)
Extracts table name from query or model for column prefixing.Parameters
Builder $query- Query builder instancemixed $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
$tableNameis 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 formatfield|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 forbetween 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
Operator-Specific Behavior
IN Operator
BETWEEN Operator
LIKE Operator
NULL Operator
ILIKEU Operator (PostgreSQL)
Error Handling
The trait throwsHttpException 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 inconfig/rest-generic-class.php:
Performance Considerations
- Table Prefixing: Automatic prefixing helps with joins but adds minimal overhead
- Operator Validation: Checking allowed operators prevents injection but requires array lookup
- Type Conversion:
decodeValue()processes each value individually - Nested Filters: Deep nesting creates subqueries; limit depth via config
Security Features
Examples
Simple Filter
Multiple Conditions
Complex Nested Logic
Using with Service
Related Documentation
- Dynamic Filtering Guide - User guide for filtering
- BaseService Class - Service that uses this trait
- Advanced Filtering Guide - Detailed examples
- Configuration - Filtering configuration