Enabling Hierarchy Support
To enable hierarchy features, define theHIERARCHY_FIELD_ID constant in your model:
Category.php
HIERARCHY_FIELD_ID must be the name of the self-referencing foreign key column (typically parent_id, manager_id, or role_id).Hierarchy Helper Methods
OnceHIERARCHY_FIELD_ID is defined, your model gains several helper methods:
List Endpoint Hierarchy Modes
Thehierarchy parameter transforms flat lists into tree structures.
Mode: roots_only
Returns only root nodes (nodes with parent_id = null):
Mode: flat_all
Returns all nodes in a flat list (default SQL behavior):
Mode: with_descendants
Returns root nodes with nested children:
Mode: with_ancestors
Returns nodes with their ancestor chain:
Show Endpoint Hierarchy Modes
Theshow endpoint supports hierarchy modes when fetching a single record.
Mode: node_only
Returns just the node with an empty children array:
Mode: with_descendants (Default)
Returns the node with all its descendants:
Mode: with_ancestors
Returns the ancestor chain from root to this node:
Mode: full_branch
Returns ancestors + node + descendants (the complete branch):
Hierarchy Configuration Options
Combining Hierarchy with Filters
Filter the tree before building the hierarchy:- Filters categories where
active = true - Builds a tree with only active categories
- Limits depth to 2 levels
Pagination with Hierarchy
Paginate root nodes, not the entire tree:Pagination applies to root nodes only. Descendants are loaded in full (up to
max_depth).Real-World Examples
E-commerce Category Tree
Build a navigation menu:Organizational Chart
Show company structure:Nested Comment Thread
Display threaded comments:Breadcrumb Trail
Get the path from root to a specific node:Performance Optimization
Problem: N+1 Queries
Without optimization, loading a tree recursively triggers one query per node.Solution: Optimized Loading
The package uses optimized queries:- Load all root nodes (1 query)
- Load all level-1 children (1 query)
- Load all level-2 children (1 query)
- Continue until
max_depth
max_depth + 1
For a tree with 1000 nodes and
max_depth=3, this is 4 queries instead of 1000.Indexing
Always index the hierarchy field:Troubleshooting
Hierarchy Not Working
Symptom: 400 error “Model does not support hierarchical listing” Cause:HIERARCHY_FIELD_ID is not defined
Solution:
Timeout on Large Trees
Symptom: Request times out with large hierarchies Causes:- No
max_depthset - Very deep tree (100+ levels)
- Missing index on hierarchy field
- Set
max_depth:"max_depth": 3 - Use pagination on roots
- Add database index on
parent_id - Filter the dataset:
"oper": {"and": ["active|=|true"]}
Circular References
Symptom: Infinite loop or stack overflow Cause: Data has circular references (A → B → A) Solution: Add database constraint to prevent circular references:Empty Children Arrays
Symptom: Leaf nodes don’t havechildren property
Cause: include_empty_children is false
Solution:
Next Steps
Advanced Filtering
Combine hierarchy with complex filters
Relation Loading
Eager-load relations in hierarchical queries
Caching
Cache tree structures for faster responses
API Reference
Complete hierarchy API reference
Evidence
-
File:
src/Core/Services/BaseService.php
Lines: 679-754, 828-925, 936-1031
ImplementsshowHierarchy(),buildShowHierarchy(),loadDescendantsOptimized(), andloadAncestorsOptimized() -
File:
src/Core/Models/BaseModel.php
Lines: 52-55, 169-240
DefinesHIERARCHY_FIELD_IDand helper methods (hasHierarchyField(),getHierarchyAncestors(),getHierarchyDescendants(), etc.) -
File:
documentacion/doc-en/03-usage/02-scenarios.md
Lines: 76-104
Shows hierarchy configuration example