cline/ancestry
Closure table hierarchies for Laravel Eloquent. Manage deep trees (org charts, categories) with O(1) ancestor/descendant queries, fluent APIs, configurable keys/types, events, and snapshots for point-in-time hierarchy state.
cline/ancestry package is a closure-table implementation for Eloquent models, enabling efficient O(1) ancestor/descendant queries—ideal for tree-like structures (e.g., org charts, category taxonomies, nested comments).HasAncestry), requiring minimal boilerplate.ancestor_id, descendant_id) to a pivot table (e.g., model_ancestries). No migrations provided—TPM must design schema.descendants(), ancestors(), isDescendantOf().(ancestor_id, descendant_id)).Maturity: readme, releases). Risk of undiscovered edge cases (e.g., circular references, deep recursion).tree package (if available) or spatie/laravel-activitylog been evaluated?descendants()) be monitored post-deployment?laravel-nestedset)?model_ancestries with columns:
ancestor_id BIGINT UNSIGNED NOT NULL
descendant_id BIGINT UNSIGNED NOT NULL
depth TINYINT UNSIGNED NOT NULL -- Optional: for depth tracking
PRIMARY KEY (ancestor_id, descendant_id)
FOREIGN KEY (ancestor_id) REFERENCES models(id)
FOREIGN KEY (descendant_id) REFERENCES models(id)
HasAncestry trait:
use Cline\Ancestry\HasAncestry;
class Category extends Model
{
use HasAncestry;
}
publish:migrations.$category->makeAncestors([1, 2]); // Sets ancestors with IDs 1, 2
// Old: Custom recursive query
// New:
$category->descendants(); // All descendants
$category->ancestors(); // All ancestors
$category->isDescendantOf($parent); // Boolean check
descendants() with depth > 10).descendants()/ancestors().depth column, ensure consistency during writes.descendants() slow?").descendants() results).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Pivot table corruption | Hierarchy queries fail |
How can I help you explore Laravel packages today?