- How do I install cline/ancestry in a Laravel project?
- Use Composer to install the package with `composer require cline/ancestry`. Then, add the `HasAncestry` trait to your Eloquent model. No migrations are included—you’ll need to manually create the pivot table (e.g., `model_ancestries`) with the required foreign keys and composite index.
- What Laravel versions does cline/ancestry support?
- The package is designed for Laravel 5.5+ (based on Eloquent’s trait system). Check the [CHANGELOG](https://github.com/faustbrian/ancestry/blob/main/CHANGELOG.md) for the latest compatibility details. If using Laravel 11+, verify SemVer compliance or test for breaking changes.
- Can I use cline/ancestry for deeply nested hierarchies (e.g., 10+ levels)?
- Yes, closure tables excel with deep hierarchies. Unlike nested sets or adjacency lists, this package provides O(1) ancestor/descendant queries regardless of depth. However, ensure your database supports composite indexes efficiently (e.g., PostgreSQL).
- How do I query all descendants of a node in cline/ancestry?
- Use the `descendants()` method on your model instance or query builder. For example: `$node->descendants()` or `Model::where('id', $nodeId)->descendants()`. This leverages the closure table for instant results without recursion.
- Does cline/ancestry work with Laravel’s soft deletes?
- Yes, if your model uses Laravel’s `SoftDeletes` trait, cline/ancestry will respect soft-deleted records in ancestor/descendant queries. The package integrates with Eloquent’s built-in soft delete logic.
- What are the performance implications of using a closure table vs. nested sets?
- Closure tables (cline/ancestry) offer O(1) ancestor/descendant queries but require additional storage (pivot table). Nested sets are simpler but suffer from O(n) depth updates. Choose closure tables for read-heavy, deep hierarchies; nested sets for shallow, write-heavy trees.
- How do I handle multiple hierarchy types (e.g., categories and tags) in one model?
- Use the `type` configuration option in `HasAncestry` to define separate hierarchies. For example: `protected $ancestryType = 'categories';`. This allows one model to participate in multiple closure-table hierarchies with distinct pivot tables.
- Is cline/ancestry thread-safe for concurrent writes (e.g., reparenting nodes)?
- The package itself doesn’t include locking mechanisms, so concurrent writes (e.g., reparenting) could lead to race conditions. For high-write scenarios, implement application-level locking (e.g., database transactions or optimistic locking) around hierarchical updates.
- Are there alternatives to cline/ancestry for Laravel hierarchies?
- Yes. For shallow trees, consider `laravel-nestedset` (simpler storage, O(n) updates). For materialized paths, use a custom solution. If using Laravel 11+, check for built-in tree support. Evaluate based on your read/write ratio and hierarchy depth.
- How do I monitor query performance with cline/ancestry in production?
- Use Laravel’s query logging (`DB::enableQueryLog()`) or database-specific tools (e.g., PostgreSQL’s `EXPLAIN ANALYZE`) to profile `descendants()`/`ancestors()` queries. Optimize with composite indexes on the pivot table (e.g., `(ancestor_id, descendant_id)`).