Overview
Rest Generic Class provides a flexible dynamic filtering system that allows clients to build complex queries using theoper parameter. The system supports:
- Multiple comparison operators (equality, range, pattern matching)
- Logical grouping with
and/orconditions - 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 theoper query parameter as a JSON object with logical operators:
field|operator|value
Supported Operators
The filtering system supports a comprehensive set of operators:Comparison Operators
| Operator | Description | Example |
|---|---|---|
= | Equals | status|=|active |
!=, <> | Not equals | status|!=|deleted |
< | Less than | price|<|100 |
> | Greater than | price|>|50 |
<= | Less than or equal | stock|<=|10 |
>= | Greater than or equal | quantity|>=|5 |
Pattern Matching
| Operator | Description | Example |
|---|---|---|
like | Case-sensitive pattern | name|like|%John% |
not like | Negated pattern | email|not like|%test% |
ilike | Case-insensitive (PostgreSQL) | title|ilike|%search% |
not ilike | Negated case-insensitive | description|not ilike|%draft% |
ilikeu | With unaccent (PostgreSQL) | name|ilikeu|Jose (matches José) |
regexp | Regular expression | code|regexp|^[A-Z]{3} |
not regexp | Negated regex | code|not regexp|[0-9] |
List Operators
| Operator | Description | Example |
|---|---|---|
in | Value in list | status|in|active,pending |
not in, notin | Value not in list | role|not in|admin,superadmin |
Range Operators
| Operator | Description | Example |
|---|---|---|
between | Value between two values | price|between|10,100 |
not between, notbetween | Value outside range | age|not between|18,65 |
Null Operators
| Operator | Description | Example |
|---|---|---|
null | Field is NULL | deleted_at|null| |
not null, notnull | Field is not NULL | email_verified_at|not null| |
Date Operators
| Operator | Description | Example |
|---|---|---|
date | Date equals | created_at|date|2024-03-05 |
not date, notdate | Date not equals | updated_at|not date|2024-01-01 |
Existence Operators
| Operator | Description | Example |
|---|---|---|
exists | Subquery exists | user_id|exists|users.id |
not exists, notexists | Subquery not exists | parent_id|not exists|categories.id |
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:Value Types
The filter system automatically decodes values:Configuration
Configure filtering behavior inconfig/rest-generic-class.php:
HasDynamicFilter Trait
The filtering logic is implemented in theHasDynamicFilter 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:Pattern Matching Search
Date Range Filtering
Security Considerations
Performance Tips
- Index filtered columns: Add database indexes to commonly filtered fields
- Limit nesting depth: Keep filter structures simple when possible
- Use specific operators:
=is faster thanlike - Avoid leading wildcards:
name|like|%termis slower thanname|like|term% - Enable column caching: Reduces validation overhead
Error Handling
Common filtering errors:Related Documentation
- Relation Loading - Filter on related models
- HasDynamicFilter Trait - Trait API reference
- Advanced Filtering Guide - Detailed examples
- Configuration - Filtering configuration options